Annotation-based declarative
In Spring, despite the use of XML configuration files can be developed to achieve AOP, but if all configurations are concentrated in the configuration file, will inevitably lead to an XML configuration file is too bloated, so bring some difficulties to maintain and upgrade.
To this end, AspectJ framework provides another way for the development of AOP development - Annotation-based declarative. AspectJ allows using the annotated section, starting point, and enhancement processing, and the frame can be identified and Spring AOP agent according to generate these annotations.
About Annotation annotations as shown in Table 1.
name | Explanation |
---|---|
@Aspect | It is used to define a slice. |
@Before | Is used to define pre-notification, equivalent BeforeAdvice. |
@AfterReturning | After notification is used to define opposed, corresponding to AfterReturningAdvice. |
@Around | It is used to define around advice, corresponding to MethodInterceptor. |
@AfterThrowing | Used to define thrown notice, the equivalent of ThrowAdvice. |
@After | Is used to define the final final notice, regardless of whether or not an exception, the notice will be executed. |
@DeclareParents | Is used to define introduce notification, an IntroductionInterceptor is equivalent (not required to master). |
The following re-implemented using annotations function "XML-based declarative" section.
1. Create a section class MyAspect
Src created under a directory named com.mengma.aspectj.annotation package to create a cut class MyAspect In this package, as follows.
package com.mengma.aspectj.annotation; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; //切面类 @Aspect @Component public class MyAspect { // 用于取代:<aop:pointcut //= expression The "Execution (. com.mengma.dao .. * * * (..))" ID = "myPointCut" /> // Requirements: The method must be private, no value, the name of the custom, no parameters @Pointcut ( " Execution (com.mengma.dao .. * *. * (..)) " ) Private void myPointCut () { } // pre-notification @Before ( " myPointCut () " ) public void myBefore (the JoinPoint Joinpoint) { . System OUT .print ( " pre-notification, goal: " ); System. OUT .print (joinPoint.getTarget () + " method name: " ); System. OUT.println (joinPoint.getSignature () getName ().); } // post notice @AfterReturning (value = " myPointCut () " ) public void myAfterReturning (the JoinPoint Joinpoint) { . the System OUT .print ( " post-notification, method name: " + . joinPoint.getSignature () getName ()); } // surround notification @Around ( " myPointCut () " ) public Object myAround (ProceedingJoinPoint ProceedingJoinPoint) throws the Throwable { the System. OUT .println ( " surround start " );// Start Object obj = proceedingJoinPoint.proceed (); // implementation of the current target method the System. OUT .println ( " Surround End " ); // End return obj; } // abnormality notifying the @AfterThrowing (value = " myPointCut () " , the throwing = " E " ) public void myAfterThrowing (the JoinPoint Joinpoint, the Throwable E) { . the System OUT .println ( " abnormality notification " + " wrong " + e.getMessage ()); } // The final notification @After ( " myPointCut () " ) public void myAfter () { the System. OUT .println ( " final notice " ); } }
The above code, line 13 @Aspect Annotations for this section is a class that is used as a component, so to add a comment @Component to take effect. Line 19 is used to configure @Poincut annotation starting point, the code unsubstituted configuration XML file entry point.
Each notice on the appropriate methods to add a comment statement, and the entry point method name "myPointCut" passed to the method to be executed as an argument for additional parameters (such as notification of abnormal abnormal parameters), can be passed in accordance with the code hints the corresponding property value.
2. Add annotations to target class
Add comment @Repository ( "customerDao") in com.mengma.dao.CustomerDaoImpl target class.
import org.springframework.stereotype.Repository; @Repository("customerDao") public class CustomerDao { public void doSome(){ // int i=1/0; System.out.println("正式业务"); } }
3. Create a Spring configuration file
Create a profile in com.mengma.aspectj.annotation applicationContext.xml package, as shown below.
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd HTTP: // www.springframework.org/schema/aop HTTP: // www.springframework.org/schema/aop/spring-aop-2.5.xsd HTTP: // www.springframework.org/schema/context HTTP: / / www.springframework.org/schema/context/spring-context.xsd "> ! <- scan all annotations containing package at com.mengma -> <context: scan-Component Base -package = " com.mengma " /> <! - enable automatic cut open agent -> <AOP: AspectJ-the autoproxy> </ AOP: AspectJ-the autoproxy> </ Beans>
The above code, first introduced constraint AOP namespace and supporting the annotation aspect class @AspectJ normal work; line scan package 13 code adds the annotation effect. Note that, here, further comprising annotation com.mengma.dao.CustomerDaoImpl target class, it is the base-package com.mengma; action code line 15 is automatically cut open the agent.
4. Create a test class
Create a test class named AnnotationTest at com.mengma.aspectj.annotation package, as shown below.
package com.mengma.aspectj.annotation; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mengma.dao.CustomerDao; public class AnnotationTest { @Test public void test() { String xmlPath = "com/mengma/aspectj/xml/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext( xmlPath); // 从spring容器获取实例 CustomerDao customerDao = (CustomerDao) applicationContext .getBean ( " customerDao " ); // perform a method customerDao.add (); } }
5. Run the project and view the results
Use JUnit test after test () method is successful, the output of the console shown in Fig.
Figure 3 operating results
Remove add () method "int i = 1/0;", re-run test () method, the output of the console shown in Figure 4 at this time.
Figure 4 run results
As it is seen from FIGS. 3 and the output 4, have been successfully used way to achieve the AOP Annotation development. Compared with other ways to achieve the effect of AOP Annotation-based approach is the most convenient way, so the actual development of the recommended way to use annotations.