Annotation-based Spring AOP sample Catalog
- Turn on support in XML configuration file
@AspectJ
- declaring Facets and pointcuts
- Notification of claims
- Test
- Conclusion
Open in XML config file
@AspectJ
Support
To use spring AOP, first add the following to the applicationContext.xml
configuration file:
<!---<AOP:aspectj-autoproxy/>
declaring Facets and pointcuts
In spring, a slice is @Aspect
a class that uses annotations. The entry point consists of two parts: the method signature and the pointcut expression. Three pointcuts are declared in the following facets.
@Aspect Public class Sampleaspect{// match all the public methods @Pointcut("Execution (Public * * (..))") Public void Publicmethod() { }// match all of the methods in the Com.app.bean package @Pointcut("Within (com.app.bean.*)") Public void Inpackage() { }// match all methods with customannotation annotations @Pointcut("@annotation (com.app.annotation.CustomAnnotation)") Public void withannotation() { }}
A third entry point uses a custom annotation class CustomAnnotation
.
Public @interface customannotation {}
Notification of claims
The next thing to declare is a notification , which is basically consistent with the structure of the facets. The difference is that the notification is used, and the @Before
@Around
annotation is in conjunction with the pointcut in the facet to determine the method of execution. The following notification example declares three ways of notifying, where @Around
a type's notification requires an ProceedingJoinPoint
instance of a class as a parameter.
@Aspect Public class Sampleadvice{@Before("Com.app.aspect.SampleAspect.publicMethod ()") Public void Advicepublicmethod() {System.out.println ("before advice matched with public methods"); }@After("Com.app.aspect.SampleAspect.inPackage ()") Public void Adviceinbean() {System.out.println ("After advice matched methods in package"); }@Around("Com.app.aspect.SampleAspect.withAnnotation ()") Public Object advicewithannotation(Proceedingjoinpoint JP)throws Throwable{System.out.println ("Around advice before proceed");Object ret= Jp.proceed (); System.out.print ("Around advice after proceed");returnRet }}
The slices and notifications that were previously declared need to be declared in the configuration file, applicationContext.xml
as shown in the final example:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdHttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsd"> <Context:Component-scan Base-package="Com.app"/><!--Start @aspectj -<AOP:Aspectj-autoproxy/><!--Facets -<Bean ID="Sampleaspect" class="Com.app.aspect.SampleAspect"/><!--Notice -<Bean ID="Sampleadvice" class="Com.app.aspect.SampleAdvice"/></Beans>
Test
Now you can write a normal class to test our code and create a file in the com.app.bean
package:
@Component Public class Commonservice {public voidservice() { System.out.println ("service method"); } Public void Transfer () { System.out.println ("Transfer method"); } @CustomAnnotation Public void Annotated () { System.out.println ("method with Annotation");} }
The test code is as follows:
Public class Test{ Public Static void Main(String[]args){ApplicationContext Context=New Classpathxmlapplicationcontext("Applicationcontext.xml");Commonservice Service= Context.getbean ("Commonservice", Commonservice.class); Service.service (); Service.transfer (); Service.annotated (); }}
Conclusion
The sample code in this article is based on the MAVEN project, and the package structure of the final code is as follows:
pom.xml
File contents:
<?XML version="1.0" encoding="UTF-8"?><Project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:XSI="http://www.w3.org/2001/XMLSchema-instance" XSI:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>com.eagle</groupId> <Artifactid>springdemo</Artifactid> <version>1.0-snapshot</version> <Dependencies> <Dependency> <groupId>junit</groupId> <Artifactid>junit</Artifactid> <version>4.11</version> </Dependency> <Dependency> <groupId>org.springframework</groupId> <Artifactid>spring-context</Artifactid> <version>4.0.4.release</version> </Dependency> <Dependency> <groupId>org.aspectj</groupId> <Artifactid>aspectjweaver</Artifactid> <version>1.8.2</version> </Dependency> <Dependency> <groupId>org.aspectj</groupId> <Artifactid>aspectjrt</Artifactid> <version>1.8.2</version> </Dependency> <Dependency> <groupId>log4j</groupId> <Artifactid>log4j</Artifactid> <version>1.2.17</version> </Dependency> </Dependencies></Project>
Annotations-based Spring AOP example