Implementation of Spring_aop_annotation:
1.1 Adding an AOP schema to Beans.xml
1.2 Open automatic retrieval of AOP in XML
<aop:aspectj-autoproxy/>
1.3 Creating a class that needs to implement a dynamic proxy
1.4 Import ASPECTJ Package, Spring uses aspect this package to implement AOP, so you need to import this package.
MAVEN's Pom.xml:
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId> <version>1.6.8</version></dependency><dependency><groupid>org.aspectj</ Groupid><artifactid>aspectjrt</artifactid><version>1.6.8</version></dependency ><dependency><groupid>aopalliance</groupid><artifactid>aopalliance</artifactid ><version>1.0</version></dependency>
1.5 Use @aspect on this class to declare that the class is a slice class
Let this slice class be managed by Spring @component ("Logaspect")//Declare this is a slice class @aspectpublic classes Logaspect { //......<span style= " font-family:fangsong_gb2312; " >}</span>
1.6 Instructions for adding pointcut to the corresponding tangency
Execution (* com.spring.dao.*.add* (..)) * The first * denotes any return value * The second * represents all classes in the Com.spring.dao package * The third * represents all methods that begin with Add * (.. ) represents any parameter
1.7 If you want to get the appropriate invocation information, you can pass the Joinpoint
/** * program starts before execution * Execution (* com.spring.dao.*.add* (..)) The first * indicates any return value * The second * represents all classes in the Com.spring.dao package the third * represents all methods (..) that begin with Add. ) represents any parameter */@Before ("Execution (* com.spring.dao.*.add* (..)) | | |" + "Execution (* com.spring.dao.*.update* (..)) | |" + "Execution (* com.spring.dao.*.delete* (..))") public void Logstart (Joinpoint JP) {//Get executed Object System.out.println (Jp.gettarget ());//method to be executed System.out.println ( Jp.getsignature (). GetName ()); Logger.info ("Add log before method execution, from Logaspect");}
Specific implementation code:
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:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema /AOP Http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-3.2.xsd "><!--Open the support of spring annotation-->< Context:annotation-config/><!--set Spring to which packages to find annotation--><context:component-scan base-package= " Com.spring "/><!--open Annotation-based AOP automatic proxy--><aop:aspectj-autoproxy/></beans>
Logaspect class
Package Com.spring.proxy;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import org.springframework.stereotype.component;//let this slice class be managed by Spring @component ("Logaspect")//Declare this is a slice class @aspectpublic Class Logaspect {/** * program starts before execution * Execution (* com.spring.dao.*.add* (..)) The first * represents any return value * The second * indicates that all classes in the Com.spring.dao package have a third * representation with a DD starts with all methods (.. ) represents any parameter */@Before ("Execution (* com.spring.dao.*.add* (..)) | | |" + "Execution (* com.spring.dao.*.update* (..)) | |" + "Execution (* com.spring.dao.*.delete* (..))") public void Logstart (Joinpoint JP) {//Get executed Object System.out.println (Jp.gettarget ());//method to be executed System.out.println ( Jp.getsignature (). GetName ()); Logger.info ("Add log before method execution, from Logaspect");} /** * After the completion of the program * @param JP/@After ("Execution (* com.spring.dao.*.add* (..)) | |" + "Execution (* com.spring.dao.*.update* (..)) | |" + "Execution (* com. spring.dao.*.delete* (..)) ") public void Logend (Joinpoint JP) {//Get executed Object System.out.println (Jp.gettarget ());//method to be executed System.out.println ( Jp.getsignature (). GetName ()); Logger.info ("Add log after method execution, from Logaspect");} /** * Function Call execution * @param PJP * @throws throwable */@Around ("Execution (* com.spring.dao.*.add* (..)) | |" + "Execution (* com.spring.dao.*.update* (..)) | |" + "Execution (* com.spring.dao.*.delete* (..))") public void Logaround (Proceedingjoinpoint pjp) throws Throwable {Logger.info ("start adding logs to around from Logaspect");// Execution procedure pjp.proceed (); Logger.info ("End Around");}}
UserService class
Package Com.spring.service;import Javax.annotation.resource;import org.springframework.stereotype.Component; Import Org.springframework.stereotype.service;import Com.spring.dao.iuserdao;import com.spring.model.User;//@ Component (value= "UserService") @Service ("UserService")//Business layer generally with @service injection public class UserService implements Iuserservice {private Iuserdao userdao;public Iuserdao Getuserdao () {return userdao;} By default injection by name, @inject is provided in JSR330 to inject//@Resource (name= "Userproxydao")//here with proxy class injection//@Resource (name= "Userdynamicdao") @ Resource (name= "Userdao")//aspect public void Setuserdao (Iuserdao userdao) {This.userdao = Userdao;} @Overridepublic void Add (user user) {userdao.add (user);} @Overridepublic void Delete (int id) {userdao.delete (id);} @Overridepublic User load (int id) {return userdao.load (ID);}}
Test class and test results:
Spring_aop_annotation using aspect to implement dynamic proxies