Spring-Boot:攔截器註解範例

來源:互聯網
上載者:User

標籤:div   參數   ons   new   str   retention   boot   定義   not   

package com.example.aop;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.Target;import static java.lang.annotation.RetentionPolicy.RUNTIME;/** * Created by liz19 on 2017/1/30. */@Target(ElementType.METHOD)@Retention(RUNTIME)@Documentedpublic @interface Action {    String name();}
package com.example.aop;import org.springframework.stereotype.Service;/** * Created by liz19 on 2017/1/30. */@Servicepublic class DemoAnnotationService {    @Action(name="註解式攔截的add操作")    public void add(){        System.out.println("clas DemoAnnotationService");    }}
package com.example.aop;import org.springframework.stereotype.Service;/** * Created by liz19 on 2017/1/30. */@Servicepublic class DemoMethodService {    public  void add(){        System.out.println("class DemoMethodService");    }}
package com.example.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;/** * Created by liz19 on 2017/1/30. */@Aspect@Componentpublic class LogAspect {    @Pointcut("@annotation(com.example.aop.Action)")    public void annotationPointCut(){}    @After("annotationPointCut()")    public void after(JoinPoint joinPoint){        MethodSignature signature = (MethodSignature) joinPoint.getSignature();        Method method = signature.getMethod();        Action action = method.getAnnotation(Action.class);        System.out.println("註解式攔截--------Name: "+action.name());    }    @Before("execution(* com.example.aop.DemoMethodService.*(..))")    public void before(JoinPoint joinPoint){        MethodSignature signature = (MethodSignature) joinPoint.getSignature();        Method method = signature.getMethod();        System.out.println("方法規則攔截攔截---------"+ method.getName());    }}
package com.example;import com.example.aop.AopConfig;import com.example.aop.DemoAnnotationService;import com.example.aop.DemoMethodService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created by liz19 on 2017/1/30. */public class AopApp {    public static void main(String[] args){        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(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;/** * Created by liz19 on 2017/1/30. */@Configuration@ComponentScan("com.example.aop")@EnableAspectJAutoProxypublic class AopConfig {}

 

1.通過@Aspect註解聲明一個切面

2.通過@Component讓此切面成為Spring容器管理的Bean

3.通過@PointCut註解聲明切點

4.通過@After註解聲明一個建言,並用@PointCut定義的切點

5.通過發射獲得註解上的屬性,然後做日誌記錄的相關操作,下面的相同

6.通過@Before註解聲明一個建言,此建言直接使用攔截規則作為參數

7.通過@EnableAspectJAutoProxy開啟對AspectJ支援

 

Spring-Boot:攔截器註解範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.