Spring AOP Usage Summary
First form: based on annotations (recommended)
@Aspect
@Component
public class myadvice{
@Before (value= "execution (* xxx.xxx.* (..))")
public void before () {
System.out.println ("pre-enhancement ...");
}
@AfterReturning (value= "execution (* xxx.xxx.* (..))")
public void after () {
System.out.println ("post Enhancement ...");
}
@Around (value= "execution (* xxx.xxx.* (..))")
Public Object around (proceedingjoinpoint Proceedingjoinpoint) throws Throwable {
System.out.println ("method before .....");
Object retVal = Proceedingjoinpoint.proceed ();//execute target method
System.out.println ("after Method .....");
Return retVal;
}
@AfterThrowing (value= "execution (* xxx.xxx.* (..))", throwing= "ex")
public void Throwingexception (xxxexception Ex) {
System.out.println ("throws XXX Exception when executing ...");
}
@After (value= "execution (* xxx.xxx.* (..))")
public void Dorelease () {
Whether the method ends normally or throws an exception, like finally, is used primarily to release resources
System.out.println ("finally dorelease ...");
}
}
Applicationcontext.xml Configuration
<!--open AOP operation--
<aop:aspectj-autoproxy/>
<!--opening annotations--
<context:component-scan base-package= "xxx.xxx.xxx"/>
------------------------------------------------------------------------------------
The second form of
public class myadvice{
public void before () {
System.out.println ("pre-enhancement ...");
}
public void after () {
System.out.println ("post Enhancement ...");
}
Surround enhancement
Public Object around (proceedingjoinpoint Proceedingjoinpoint) throws Throwable {
Method before
System.out.println ("method before .....");
Execute the Enhanced method
Object ret = Proceedingjoinpoint.proceed ();
Method after
System.out.println ("after Method .....");
Return ret;
}
Exception enhancement
public void Throwingexception () {
System.out.println ("execute when exception throws ...");
}
}
Applicationcontext.xml Configuration
<!--1 Configuring Advice objects--
<bean id= "myadvice" class= "xxx.xxx.MyAdvice" ></bean>
<!--2 Configuring AOP operations--
<aop:config>
<!--2.1 Configuring pointcuts--
<aop:pointcut expression= "execution (* xxx.xxx.*.* (..))" id= "pc"/>
<!--2.2 Configuration facets--
<aop:aspect ref= "myadvice" >
<aop:before method= "before" pointcut-ref= "pc"/>
<aop:after-returning method= "after" pointcut-ref= "pc"/>
<aop:around method= "around" pointcut-ref= "pc"/>
<aop:after-throwing method= "throwingexception" pointcut-ref= "pc"/>
</aop:aspect>
</aop:config>
----------------------------------------------------------------------------------------
Third Form: implementing interfaces
1 "front-mounted enhancement function
public class Beforeadvice implements methodbeforeadvice{
/**
* Executed before the target method
* Method: Target Methods Object
* Args: parameters of the method
* Target: the targeted object being proxied
*/
@Override
public void before (method, object[] args, Object Target) throws Throwable {
}
}
2 "rear-mounted Enhancements
public class Afteradvice implements afterreturningadvice{
Executes after the target method
@Override
public void Afterreturning (object ret, method method, object[] args, object Target) throws Throwable {
}
}
3 "surround Enhancements
public class Aroundadvice implements methodinterceptor{
Methodinvocation: Target Method Object
@Override
Public Object invoke (methodinvocation Methodinvocation) throws Throwable {
System.out.println ("before method");
Object retVal = Methodinvocation.proceed ();//call target method
System.out.println ("after method");
Return retVal;
}
}
4. Enhanced function when abnormal (learn)
public class Exceptionadvice implements Throwsadvice {
Executes when an exception is thrown in the target
Ex: thrown Exception object
@Override
public void afterthrowing (Exception Ex) {
}
}
Applicationcontext.xml Configuration
<!--1 Configuring Advice objects--
<bean id= "beforeadvice" class= "xxx.xxx.BeforeAdvice" ></bean>
<bean id= "afteradvice" class= "xxx.xxx.AfterAdvice" ></bean>
<bean id= "aroundadvice" class= "xxx.xxx.AroundAdvice" ></bean>
<bean id= "exceptionadvice" class= "xxx.xxx.ExceptionAdvice" ></bean>
<!--2 Configuring AOP operations--
<aop:config>
<!--2.1 Configuring pointcuts--
<aop:pointcut expression= "execution (* xxx.xxx.*.* (..))" id= "pc"/>
<!--2.2 Configuration facets--
<aop:advisor advice-ref= "beforeadvice" pointcut-ref= "pc"/>
<aop:advisor advice-ref= "afteradvice" pointcut-ref= "pc"/>
<aop:advisor advice-ref= "aroundadvice" pointcut-ref= "pc"/>
<aop:advisor advice-ref= "exceptionadvice" pointcut-ref= "pc"/>
</aop:config>
Spring AOP Usage Summary