PackageCOM.EXAMPLE.AOP;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.Target;Import StaticJava.lang.annotation.RetentionPolicy.RUNTIME;/*** Created by liz19 on 2017/1/30.*/@Target (Elementtype.method) @Retention (RUNTIME) @Documented Public@InterfaceAction {String name ();}
package COM.EXAMPLE.AOP; import Org.springframework.stereotype.Service; /** * Created by liz19 on 2017/1/30. */ @Service public class Demoannotationservice {@Action (name = "Add operation for annotated interception" ) public void add () {System.out.println ( "Clas demoannotationservice"
Package COM.EXAMPLE.AOP; Import Org.springframework.stereotype.Service; /** */@Servicepublicclass demomethodservice { public void Add () { System.out.println ("class Demomethodservice");} }
PackageCOM.EXAMPLE.AOP;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.aspectj.lang.reflect.MethodSignature;Importorg.springframework.stereotype.Component;ImportJava.lang.reflect.Method;/*** Created by liz19 on 2017/1/30.*/@Aspect @component Public classlogaspect {@Pointcut ("@annotation (com.example.aop.Action)") Public voidannotationpointcut () {} @After ("Annotationpointcut ()") Public voidAfter (Joinpoint joinpoint) {methodsignature signature=(methodsignature) joinpoint.getsignature (); Method Method=Signature.getmethod (); Action Action= Method.getannotation (Action.class); System.out.println ("Annotated interception--------Name:" +action.name ()); } @Before ("Execution (* com.example.aop.demomethodservice.* (..))") Public voidbefore (Joinpoint joinpoint) {methodsignature signature=(methodsignature) joinpoint.getsignature (); Method Method=Signature.getmethod (); System.out.println ("Method rules intercept interception---------" +method.getname ()); }}
Packagecom.example;ImportCom.example.aop.AopConfig;ImportCom.example.aop.DemoAnnotationService;ImportCom.example.aop.DemoMethodService;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Created by liz19 on 2017/1/30.*/ Public classAopapp { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Aopconfig.class); Demoannotationservice Demoannotationservice= Context.getbean (Demoannotationservice.class); Demomethodservice Demomethodservice= Context.getbean (Demomethodservice.class); Demoannotationservice.add (); Demomethodservice.add (); Context.close (); }}
Package COM.EXAMPLE.AOP; Import Org.springframework.context.annotation.ComponentScan; Import org.springframework.context.annotation.Configuration; Import Org.springframework.context.annotation.EnableAspectJAutoProxy; /** */@Configuration @componentscan ("COM.EXAMPLE.AOP") @EnableAspectJAutoProxy Publicclass aopconfig {}
1. Declare a slice with @aspect annotations
2. Make this facet a spring container-managed bean by @component
3. Declaring pointcuts through @pointcut annotations
4. Declare a statement by @after annotations and use the tangent point defined by the @pointcut
5. Obtain the attributes on the annotation by firing and do the related operation of the log record, the following same
6. Declaration of a statement through the @before annotation, which uses interception rules directly as parameters
7. Turn on ASPECTJ support via @EnableAspectJAutoProxy
Spring-boot: Examples of interceptor annotations