What is Spring AOP – Let’s explore

Spring AOP

Spring Series – Learn Spring AOP by Example

AOP Basics

In today’s complex application development the following points are always kept as a focus area.

  • Application should be developed as a combination of modular components.
  • All the components should be loosely coupled.
  • Components should be reusable.

The advantage of this approach is

  • Easy maintenance and enhancement of the application.
  • Components can be reused in the existing or new application.
  • Reduces the cost of maintenance and enhancement.

Now to address these concerns spring has developed the concepts like IOC and AOP. We have already discussed IOC so now we will concentrate on AOP.

Spring AOP

AOP targets the application functionalities (like logging, transaction etc) which are used throughout the application in different areas. Using AOP these cross-cutting 功能被開發為可重用組件並注射到以往所需要的應用程序,,en,又一次的整個配置是使用XML文件來完成,,en,AOP的實現,,en,下圖顯示了旅遊管理系統,,en,訂票“,,en,訂酒店',,en,旅遊規劃,,en,'是不同的功能,,en,現在,所有這些功能都需要像日誌服務,,en,事務和安全性,,en,因此,這些都是橫切關注其遇到的所有模塊,,en,這些問題都開發了基於AOP的原則,,en,所有共同關心的問題被開發作為一個插件,,en,因此它可以在未來的模塊一起使用,以及,,en,你只需要對其進行配置,以便服務也是在新的模塊中可用,,en,這是面向方面的編程的力量,,en,AOP,,en. And again the entire configuration is done using xml file.

Implementation of AOP

Following diagram shows a travel management system. ‘Ticket Booking’, ‘Hotel Booking’ 和“Travel Planning’ are different functionalities. Now all of these functionalities require services like logging, transaction and security. So these are cross cutting concerns which come across all the modules. These concerns are developed based on the principle of AOP.

All the common concerns are developed as a plug-in, so it can be used in future modules as well. You just need to configure it so that the service is also available in your new modules. This is the power of aspect oriented programming (AOP).

AOP is also applied in enterprise level applications. In enterprise application, 交易, logging, security and many more features are there as cross cutting concerns. So all of these are developed as aspects and configured in xml file.

The biggest advantage is loose coupling. As all the concerns are self-managed and independent, so you can change and enhance them as per your requirement without touching the application code.







Travel Management System

Now we will describe the AOP implementation by showing an example.

To start with, first we will define an interface and its implementation class. This is a simple application to compare two integer values and return the result.

Java is the interface definition to compare two integer values

package com.techalpine;

public interface CompareInt{

public String compare(int a,int b);

}

Java is the implementation class. 它比較兩個整數值,並返回結果,,en,公共類CompareIntImpl實現CompareInt,,en,返回 “大於” B,,en,b為大於a,,en,現在,我們將編寫一個簡單的建議調用比較之前調用,,en,該建議將在細節在下面的章節中討論,,en,現在你可以想像意見,這將通過xml配置被插入到應用程序中的任務,,en,而這將是調用之前調用該方法,,en,但不會有實現類內的任何直接編碼,,en,我們只將實現類的建議之前採取,,en,進口的java.lang.reflect.Method,,en,進口org.springframework.aop.MethodBeforeAdvice,,en,公共類LogBefore實現MethodBeforeAdvice,,en,前公共無效,,en,目標對象,,en,前調用的方法比較,,en

package com.techalpine;

public class CompareIntImpl implements CompareInt{

public String compare(int a, int b){

如果 (一>b)

{

return a ” is greater than” b;

}

其他

{

return “b is greater than a”;

}

}

}

Now we will write a simple advice to be called before calling the compare (詮釋, 詮釋) 方法. The advice will be discussed in details in the following chapters. Now you can imagine advice as a task which will be plugged into the application through xml configuration. And this will be called before the method is called. But there will not have any direct coding inside the implementation class. We will only take before advice for the implementation class.







package com.techalpine;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class LogBefore implements MethodBeforeAdvice{

public void before(Method method, 目的[] 參數, Object target) {

System.out.println(“Before Calling the compare Method”);

}

}

現在,我們將討論有關配置文件,,en,測試aop.xml文件,,en,該文件有三個部分,,en,第一部分是建議bean定義,,en,所述第二部分限定了實現類,,en,第三部分定義了建議的結合與實現類,,en,www.springframework.org/schema/beans/spring-beans-2.0.xsd,,en,建議,,en,豆ID =,,es,beforeCall,,en,CLASS =,,en,com.techalpine.LogBefore,,pt,實現類,,en,compareImpl,,en,com.techalpine.CompareIntImpl,,pt,代理實現類,,en,比較,,en,的org.springframework.aop.framework.ProxyFactoryBean,,en,屬性名=,,en,proxyInterfaces,,en,com.techalpine.CompareInt,,pt,interceptorNames,,en,REF =豆,,es,現在,最後一部分是測試類CompareTest.java,,en,它通過使用XmlBeanFactory的類加載bean定義,然後叫比較,,en,進口org.springframework.beans.factory.BeanFactory,,en,進口org.springframework.beans.factory.xml.XmlBeanFactory,,en (Test-AOP.xml)

The file has three sections

  • The first section is the advice bean definition
  • The second section defines the implementation class
  • The third section defines the binding of the advice with the implementation class

<beans xmlns=”HTTP://www.springframework.org/schema/beans”

xmlns:xsi=”HTTP://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”HTTP://www.springframework.org/schema/beans

HTTP://www.springframework.org/schema/beans/spring-beans-2.0.xsd”>

<!– Advices –>

<bean id = “beforeCall”

class = “com.techalpine.LogBefore” />

<!– Implementation Class –>

<bean id = “compareImpl”

class = “com.techalpine.CompareIntImpl ” />

<!– Proxy Implementation Class –>

<bean id = “compare”

class = “org.springframework.aop.framework.ProxyFactoryBean”>

<property name = “proxyInterfaces”>

<值>com.techalpine.CompareInt</值>

</財產>

<property name = “interceptorNames”>

<名單>

<值>beforeCall</值>

</名單>

</財產>

<property name = “target”>

<ref bean = “compareImpl”/>

</財產>

</bean>

</beans>

Now the last part is the test class CompareTest.java. It loads the bean definition by using XMLBeanFactory class and then called the compare () 方法.

package com.techalpine;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.*;

public class CompareTest {

公共靜態無效的主要(字符串參數[]){

Resource resource = new FileSystemResource(“./src/Test-AOP.xml”);

BeanFactory factory = new XmlBeanFactory(resource);

CompareInt com = (Adder)factory.getBean(“compare”);

String result = com.compare(12,10);

System.out.println(“Result = ” + result);

}

}

The output of the compare () method call would be as follows

Before calling the compare Method

12 is greater than 10







結論:

Spring AOP is very helpful when we try to separate business logic from cross cutting concerns. It is widely used in different forms to cover various aspects. AOP can also be implemented in other programming languages.

 

標籤:
============================================= ============================================== 在亞馬遜上購買最佳技術書籍,en,電工CT Chestnutelectric,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share