Spring-boot: Examples of interceptor annotations

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.