When using spring AOP, it is necessary to determine the annotations on the target method, such as caching, authentication permissions, etc.
Custom annotations
Packagecom.agent.annotation;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;Importorg.springframework.stereotype.Component, @Component @retention (retentionpolicy.runtime) @Target ( Elementtype.method) Public@Interfacemyannotation { Public BooleanIsenable ()default true;}
AspectJ of Spring AOP
PackageCOM.AGENT.AOP;ImportJava.lang.reflect.Method;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;Importorg.springframework.stereotype.Component;Importcom.agent.annotation.MyAnnotation, @Component @aspect Public classlogutil {@Around ("@annotation (com.agent.annotation.MyAnnotation)") PublicObject logwrited (Proceedingjoinpoint point)throwsthrowable {object[] args=Point.getargs (); Class<?>[] Argtypes =NewClass[point.getargs (). length]; for(inti = 0; i < args.length; i++) {Argtypes[i]=Args[i].getclass (); } Method=NULL; Try{method=point.gettarget (). GetClass (). GetMethod (Point.getsignature (). GetName (), argtypes); } Catch(nosuchmethodexception e) {e.printstacktrace (); } Catch(SecurityException e) {e.printstacktrace (); } myannotation ma= Method.getannotation (myannotation.class); System.out.println (Ma.isenable ()); returnpoint.proceed (); } }
Service Interface
Package Com.agent.service; Public Interface userservice { void addUser (string name, string password);
Implementation classes for service interfaces, annotated by custom annotations
PackageCom.agent.service.impl;ImportOrg.springframework.stereotype.Service;Importcom.agent.annotation.MyAnnotation;Importcom.agent.service.UserService; @Service (Value= "UserService") Public classUserserviceimplImplementsuserservice {@Override @MyAnnotation Public voidAddUser (string name, string password) {System.out.println ("Userserviceimpl.adduser () ... name:" + name + "; Password: "+password); }}
Test class:
Packagecom.agent.test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.agent.service.UserService; Public classAoptest { Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); UserService US= (UserService) ac.getbean ("UserService"); Us.adduser ("Zhang San", "188"); }}
Spring's configuration file:
<?XML version= "1.0" encoding= "Utf-8"?><Beansxmlns= "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"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/s Pring-beans.xsd Http://www.springframework.org/schema/context http:/ /www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schem A/AOP http://www.springframework.org/schema/aop/spring-aop.xsd http: Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <Context:component-scanBase-package= "Com.agent" /> <!--<bean id= "aspect" class= "Com.agent.aop.LogUtil"/> <aop:config> <aop:aspect ref= "aspect" > <aop:pointcut expression= "Execution (* add* (..))" id= "Mypointcut"/> <aop:after method= " Logwrited "pointcut-ref=" Mypointcut "/> <aop:around method=" logwrited "pointcut-ref=" Mypointcut "/> </aop:aspect> </aop:config> - <Aop:aspectj-autoproxy/></Beans>
Test results:
If you are using the mode of an interface, and the annotation is on the implementation class, you cannot get the object of the target method as follows, because it gets the object of the class's interface or the method of the top-level parent class
Methodsignature methodsignature = (methodsignature) point.getsignature (); = Methodsignature.getmethod ();
Spring: How to get annotations on a target method when using spring AOP