1. Introduction to Spring AOP
Spring provides a number of ways to implement AOP, with spring interface, schema configuration and annotations in three ways, starting with Spring 2.0, you can use schema-based and @aspectj to implement AOP, This article introduces a simple example of how to implement AOP in spring in a @aspectj way. Since @aspect is annotation-based, it is required to support more than 5.0 versions of the JDK for annotations.
2.Spring AOP Environment
To use spring AOP in your project, you will need to import in your project, in addition to the Spring jar package, Aspectjweaver.jar,aspectjrt.jar and Cglib.jar.
In spring MVC basically just add Aspectjweaver.jar and Cglib.jar. As follows:
<!--AspectJ AOP -<Dependency> <groupId>Org.aspectj</groupId> <Artifactid>Aspectjweaver</Artifactid> <version>${aspectj.version}</version></Dependency><Dependency> <groupId>Cglib</groupId> <Artifactid>Cglib</Artifactid> <version>${cglib.version}</version></Dependency>
3. Example
This example uses AOP to implement a post-execution enhancement of the execution method, which is to execute a method we have written after executing the target method to process the relevant data.
A.spring.xml Configuration
<!---<aop:aspectj-autoproxy/>
B. Target method code
PublicResponsemsg Submitresult (intTASK_ID,LongBatchbyteresult) { Try { if(Result < 0 | | result > 1) {Log.info ("Result parameter is illegal, result:" +result); return Newresponsemsg (ARGUMENT); } taskdao.updatetaskres (Task_id,batch, (byte) 5,NULL, result); return Newresponsemsg (SUCCESS); } Catch(Exception e) {e.printstacktrace (); Log.error (E.getmessage ()); return Newresponsemsg (FAILURE); } }
C. Surround enhanced @aspectj Code
@Aspect @component Public classQualitycheckinterceptor {Private Static FinalLogger log = Loggerfactory.getlogger (qualitycheckinterceptor.class); @AfterReturning (Pointcut= "Execution (* Com.iflytek.service.Impl.QualityCheckServiceImpl.submitResult (..)) && args (*,batch,..) ") Public voidCheckState (LongBatch) { Try{Integer count= Taskdao.getcount (NULL,NULL, Batch, (byte) 4,NULL); if(Count > 0){return; } Integer correct= Taskdao.getcount (NULL,NULL, Batch,NULL,(byte) 1); Integer Incorrect= Taskdao.getcount (NULL,NULL, Batch,NULL,(byte) 0); Tasksamplingdao.updatesampling (Batch,correct,incorrect, (byte) 1); intCorrect_rate = correct */(correct +incorrect); if(Correct_rate >=THRESHOLD) {communiteresult (Batch,NULL); }Else {batchdao.updatebatch (Batch,NULL,(byte) 4); Taskdao.init (Batch); } } Catch(Exception e) {e.printstacktrace (); Log.error (E.getmessage ()); } }}
This achieves the required functionality, the advantage of AOP is that the target method code does not make any changes, you can achieve before and after processing, in addition to C can also use @before, @After and other annotations to achieve different functions.
4. Detailed usage of @AspectJ
There is currently only one connection point for execution methods in spring AOP, and the ASPECTJ pointcut indicator supported by spring AOP is as follows:
Examples of some common pointcuts
Execution (Public * * (..)) When any public method is executed, the Pointcut function is executed.
Execution (* set* (..)) Any method that starts with a "set" is executed, and the Pointcut function is executed.
Execution (* com.demo.service.accountservice.* (..)) When any method in interface Accountservice is executed, the Pointcut function is executed.
Execution (* com.demo.service.*.* (..)) When any method in the service pack is executed, the Pointcut function is executed. Within (com.demo.service.*) any connection point in the service package. Within (com.demo.service. *) at any connection point in the service package or child package.
This (Com.demo.service.AccountService) implements any connection point for the proxy object of the Accountservice interface.
Target (Com.demo.service.AccountService) implements any connection point of the target object of the Accountservice interface.
Args (java.io.Serializable) accepts only one parameter, and at run time the incoming parameter implements the connection point of the Serializable interface
Enhanced way:
@Before: Execute Before method
@AfterReturning: Execute after running the method
@AfterThrowing: Execute after throw
@After: Executes (similar to finally) regardless of how the method ends
@Around: Wrapping Execution
Spring Annotations @aspectj way to implement AOP