Implementation steps:
1: Import a class-scan annotation Parser
Namespaces: xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd "
The XML configuration file is configured as follows: <context:component-scan base-package= "Com.lxk.spring.aop.annotation"/>
2: Import SPRINGAOP annotation Parser
Namespaces: xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
Xsi:schemalocation= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "
The XML configuration file is configured as follows:<aop:aspectj-autoproxy/>//turn on AOP annotation Auto Proxy
3: First define our class to implement a function in the project.
1 import org.springframework.stereotype.Component; 2 3 @Component ("Aopfun" ")//load this class into the bean, here is the annotated form used, or you can configure <bean id= "Aopfun" class= "COM.ZS.AOP" in the Spring.xml file >& Lt;/bean> input and Dependency injection 4 public class aopfun{ 5 public void Printfstr () { 6 System.out.println (" Perform tangent method " 7 } 8 }
4: Configure the Slice class, which is the configuration under what circumstances we are going to perform the tangency:
1 /**2 * Annotated way to declare AOP3 * 1. Use @aspect annotations to declare a class as a slice (if you annotate a bean object with a @component ("") annotation, you must turn on the annotation scan in the spring configuration file <context:component-scan Base-package= "Com.cjh.aop2"/>4 * Otherwise declare a Bean object in the Spring configuration file)5 * 2. Add the appropriate comment before the slice needs to implement the corresponding method, that is, the notification type. 6 * 3. There is a surround notification, the surround notification method must have Proceedingjoinpoint type parameters passed in, and then execute the corresponding proceed () method, wrapping can be achieved. 7 */8@Component ("Aoptest")9 @AspectTen Public classaoptest{ One //Defining Pointcuts A@Pointcut ("Execution (* *.printfstr (..))") - Public voidprintfstring () {} - /** the * Pre-notification (the Printfstring () method in the annotation is actually the method name that defines the pointcut pointcut annotation, which is just a proxy object, does not need to write a specific method, - * Equivalent to the ID name of the XML declaration slice, as below, equivalent to id= "embark", for other notification type reference) - * <aop:config> - <aop:aspect ref= "Mistrel" > + <!--defining pointcuts -- - <aop:pointcut expression= "Execution (* *.printfstr (..))" id= " embark"/> + <!--declaring a pre-notification (called before the Tangency method is executed)- A <aop:before method= "Beforsay" pointcut-ref= "Embark"/> at <!--declaring a post-notification (called after the tangent method is executed)- - <aop:after method= "Aftersay" pointcut-ref= "Embark"/> - </aop:aspect> - </aop:config> - */ -@Before ("printfstring ()")//before: Executes before the tangent is executed in Public voidSayHello () { -System.out.println ("Note type pre-notification"); to } + //Post Notification -@After ("printfstring ()")//after: Executes after the tangent point execution the Public voidSaygoodbey () { *System.out.println ("Note type post notification"); $ }Panax Notoginseng //surround notifications. Note that you want to have the Proceedingjoinpoint parameter passed in. -@Around ("printfstring ()") the Public voidSayaround (Proceedingjoinpoint PJP)throwsthrowable{ +System.out.println ("Annotation type wrapping Notification: Surround Front "); APjp.proceed ();//Execution Method theSystem.out.println ("Annotation type wrapping Notification: Surround "); + } -}
5:spring.xml file Configuration
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd8http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd">9<!--open annotation Scan--Ten<context:component-scan base- Package= "Com.zs.aop"/> One<!--turn on AOP annotations, this step s is not so small that the AOP annotations in the Java class take effect- A<aop:aspectj-autoproxy/> -</beans>
6: Test class
1 PackageCom.cjh.aop2;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7 * 8 * @authorCaijh9 * Email:[email protected]Ten * July 11, 2017 afternoon 6:27:06 One */ A Public classTest { - Public Static voidMain (string[] args) { -ApplicationContext AC =NewClasspathxmlapplicationcontext ("Com/zs/aop/spring.xml"); Get ApplicationContext Object theAopfun bean1= (Aopfun) Ac.getbean ("Aopfun"); Get the Aopfun object through spring's bean object and make dependency injection on the object - Bean1.printfstr (); Execution Function Method - } -}
7: The result of the final printing is
annotation type Wrapping Notification: Surround Front
Note Type pre-notification
Execute Tangency method
annotation type Wrapping Notification: After wrapping
Note Type post-placement notification
The notification types are:
1: @Before: Notifies this method to be executed before the function
2: @After: Notifies this method to be executed after the function
3: @AfterThrow: Notifies this method to execute when the function throws an exception
4: @AfterReturning: Notifies this method to execute after function return
5: @Around: Notify this method will encapsulate the function.
In the end, just a little bit. Configuring AOP with Spring.xml files
1<!--configuring AOP with XML--2<!--enforces the use of the Cglib proxy, and if not set, the JDK's proxy will be used by default, but the JDK's proxy is interface-based--3<aop:config proxy-target-class= "true"/>4<aop:config>5<!--defining Facets--6<aop:aspect id= "Logaspect" ref= "Loginterceptor" >7<!--defining Pointcuts (the configured Printfstr method will be intercepted before the call)--8<aop:pointcut expression= "Execution (Execution (* *.printfstr (..))" id= "Logpointcut"/>
9<!--method execution was called before the execution of the
Ten<aop:before method= "Before" pointcut-ref= "Logpointcut"/><!--a pointcut reference--
One<aop:after method= "after" pointcut-ref= "Logpointcut"/><!--a pointcut reference--
A</aop:aspect>
-</aop:config>
Spring AOP Annotations Form Simple implementation