1, AOP, aspect-oriented programming (aspect Oriental programing), using AOP, you can inject the code that handles the facets aspect into the main program, usually the main purpose of the main program is not to handle these facets aspect, which prevents code clutter. Interceptor Interceptor is another term for AOP. (where the mode used is proxy mode, dynamic proxy mode).
2, notify advise: In the aspect of a connection point (in spring AOP, a connection point is the execution of a method) on the action performed.
Pointcut Pointcut: The assertion that matches the connection point. The notification is associated with a pointcut expression and runs on the connection point that satisfies the pointcut.
The target object: target object, which is notified by a live multiple slice. Are called also notified objects.
3. AOP in Spring
Add a jar package that supports AOP:
Aopalliance.jar
Aspectjrt.jar
Aspectjweaver.jar
Spring-aop.jar
Spring-aspect.jar
(1) Add an AOP namespace to the XML:
<?xml version= "1.0" encoding= "UTF-8"?><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 " xsi:schemalocation=" http://www.springframework.org/schema/ Beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>
(2) Notification of writing aspect classes and facets
class= "Com.spring1.util.Aspect"/> <aop:config> <!--facets-- <aop:aspect ref = "Myaspect" > <!--pointcut Execution (method access modifier?) method return type declaration class? Method name (method parameter type) throws an exception?-- <aop:pointcut expression = "Execution (* Com.spring1.dao). *.*(..))" Id= "Point1"/> <aop:before method= "beforeadvise" pointcut-ref= "Point1"/> <aop:after method= " Afteradvise "pointcut-ref=" Point1 "/> <!--return notification, the last parameter is the name of the notification parameter--- <aop:after-returning method=" Afterreturning "pointcut-ref=" Point1 "returning=" ret "/> <!--exception notification, the last parameter is the name of the exception parameter--- <AOP: After-throwing method= "afterthrowing" pointcut-ref= "Point1" throwing= "E"/> </aop:aspect> </ Aop:config>
(3) handing the aspect class to spring management, configuring the bean of the aspect class
The top has been configured
(4) Write notification functions according to the notification in AOP:
//Slice class Public classAspect {//notice ... voidbeforeadvise () {System.out.println ("Before"); } voidafteradvise () {System.out.println ("After"); } voidafterreturning (Object ret) {System.out.println ("Afterreturning" +ret.tostring ()); } voidafterthrowing (Exception e) {System.out.println ("Afterthrowing" +e.getmessage ()); }}
(5) When the top of the need for some time can be directly replaced by surround notice:
<bean id= "Myaspect" class = "Com.spring1.util.Aspect"/> <AOP: Config> <!--facets--<aop:aspect ref= "Myaspect" > <!--pointcut Execution (method access modifier? method return type declaration Class? Method Name (method parameter type) throws an exception?) --<aop:pointcut expression= "Execution (* Com.spring1.dao. *.*(..))" Id= "Point1"/> <aop:around pointcut-ref= "point1" method= "Aroundadvise"/> </aop:aspect> </aop:config>
void aroundadvise (Proceedingjoinpoint pjp) { try { System.out.println ("front"); = pjp.proceed (); System.out.println (Object.ToString ()); Catch (Throwable e) { e.printstacktrace (); System.out.println ("Exception"); } finally { System.out.println ("final"); } }
The post notification is the notification after the return
The final notification is also called a finally notification.
Spring AOP Programming