Prerequisites :
Import the jar required for AOP:
Aspectjweaver.jar and Aspectjrt.jar and Cglib-nodep-2.1_3.jar
namespaces required for joining AOP:
xmlns:aop="http://www.springframework.org/schema/aop" http:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Spring provides two ways to use facets:
Based on annotation and XML-based methods.
development of AOP based on annotations :
Premise:
Precede with an AOP declaration
Open support for @aspect:<aop:aspectj-autoproxy/>
<beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-2.5.xsd "><context:component-scanBase-package="Cn.itcast"/> <aop:aspectj-autoproxy/>
Some notes about:
@Aspect declaring slices, acting on the class
@Pointcut Declaration entry point
Example:
@Pointcut ("Execution (* cn.itcast.service.impl.personservicebean.* (..)) " private void anymethod () {}// declares a pointcut in the form of a method definition
Note the point:
1, the Pointcut Declaration and method is similar, but the name of the Pointcut is the method name + (), such as the above pointcut named Anymethod ()
2. The scanning content format of pointcut is basically fixed: execution (pointcut expression)
Pointcut expression:
Decomposition: Returns the value type package name. Class Name. Method name (method parameter)
Available symbols:
* wildcard character, which means any
.. Two meanings, one representing a variable parameter, used in a parameter list, one representing: A child package under the package and the package, using the
! Non-
Pointcut Expression Examples:
The return value is of type string:
Execute (java.lang.String cn.itcast.service. *.*(..))
The first parameter that requires an input parameter is a string type:
Execute (* cn.itcast.service. * * (java.lang.String,..))
Required return value is not void:
Execute (! void Cn.itcast.service. *.*(..))
Notification function on the method
@Before Front-facing notifications
@Before ("anymethod ()")
If you want to get the parameters in the pointcut that you intercepted, you can:
@Before (value="anymethod () && args (name)")publicVoid Dobefore (String name) {// Pre-notification, the parameter inside is the pointcut name System. out. println (" pre-notification "+name);}
@AfterReturning Post Notification
@AfterReturning ("anymethod ()"
If you want to get the return value of the method in the post-notification:
@AfterReturning (pointcut="anymethod ()", returning="result" publicvoid doafterreturning (String result) {System. out. println (" post notification "+result);
@After Final Notice
@After ("anymethod ()")publicvoid doafter () {S ystem. out. println (" Final Notice ");
@AfterThrowing Exception Notification
@AfterThrowing ("anymethod ()")
If you want exception information to be in an exception notification:
@AfterThrowing (pointcut="anymethod ()", throwing="e") publicvoid doafterthrowing (Exception e) {System. out. println (" exception notification "+e);}
@Around Surround Notifications
@Around ("Anymethod ()") PublicObject Dobasicprofiling (Proceedingjoinpoint pjp) throws throwable{Object result=NULL; if(true){//determine if the user has permissionsSystem. out. println ("Entry Method"); Result=pjp.proceed ();//This method is similar to DofilterSystem. out. println ("Exit Method"); }returnresult;}
Attention:
1, the action method of the surround notification format is unchanged, only the method name and parameter name variable
2. Surround notification is similar to the interceptor in Struts2. The proceed () method of the Proceedingjoinpoint object is similar to the Invocation.invoke () method
AOP development based on XML :
Our original slice became a common Java class:
Package Cn.itcast.service;import Org.aspectj.lang.proceedingjoinpoint;import org.springframework.stereotype.Component; @Component ("Aspectbean") Public classMyinterceptor { Public voidDobefore () {//front-facing notificationsSystem. out. println ("front-facing notifications"); } Public voiddoafterreturning (String result) {System. out. println ("Post Notification"+result); } Public voidDoafter () {System. out. println ("Final Notice"); } Public voiddoafterthrowing (Exception e) {System. out. println ("Exception Notification"+e); } PublicObject Dobasicprofiling (Proceedingjoinpoint pjp) throws throwable{Object result=NULL; if(true){//determine if the user has permissionsSystem. out. println ("Entry Method"); Result=pjp.proceed ();//This method is similar to DofilterSystem. out. println ("Exit Method"); } returnresult; } }
View Code
Beans.xml of the corresponding configuration:
<aop:config> <aop:aspect id="ASP" ref="Aspectbean"> <aop:pointcut id="Mycut"expression="Execution (* cn.itcast.service.impl). *.*(..))"/> <aop:before method="Dobefore"pointcut-ref="Mycut"/> <aop:after-returning method="doafterreturning"pointcut-ref="Mycut"returning="result"/> <aop:after method="Doafter"pointcut-ref="Mycut"/> <aop:after-throwing method="doafterthrowing"pointcut-ref="Mycut"throwing="e"/> <aop:around method="dobasicprofiling"pointcut-ref="Mycut"/> </aop:aspect> </aop:config>
Pre-notification parameters get using XML configuration I did not get out, do not turn niu Jiao Jian, know can tell the next, thank you!
Spring AOP Programming