1. 程式人生 > >springboot-3-aop

springboot-3-aop

bsp log int onf enc span work artifact param

aop存在的目的是進一步解耦, spring支持aspectJ的註解式切面編程

1), [email protected], [email protected]

2), [email protected], @Before, @Aroud定義advice, 可直接引入 pointcut

代碼實現:

1, 引入aspectJ的依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>       
 <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8
.6</version> </dependency>

2, 自定義註解

package com.wenbronk.aop;

import org.springframework.stereotype.Component;

/**
 * Created by wenbronk on 2017/5/13.
 */
@Component
public class InterceptAnnotation {

    @AopAnnotation(value = "註解攔截")
    public void intercept() {
        System.out.println("
annotation intercepting"); } }

3, 自定義方法攔截

package com.wenbronk.aop;

import org.springframework.stereotype.Component;

/**
 * 方法攔截
 * Created by wenbronk on 2017/5/13.
 */
@Component
public class IntercepterMethod {
    public void intercepter() {
        System.out.println("method intercepter");
    }
}

4, 編寫切點

package com.wenbronk.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 wenbronk on 2017/5/13.
 */
@Aspect
@Component
public class AopAspect {

    @Pointcut(value = "@annotation(com.wenbronk.aop.AopAnnotation)")   // 切點為註解
    public void pointCut() {};

    /**
     * 註解式攔截, [email protected], @Before, @Aroud中直接引入 @pointCut
     * @param joinPoint
     */
    @After(value = "pointCut()")
    public void afterPointCut(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        AopAnnotation annotation = method.getAnnotation(AopAnnotation.class);
        System.out.println("註解式攔截: " + annotation.value());
    }


    /**
     * 方法式攔截
     * @param joinPoint
     */
    @Before(value = "execution(* com.wenbronk.aop.IntercepterMethod.*(..))")
    public void beforePointCut(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法式攔截: " + method.getName());
    }

}

5, javaconfig

如果你使用的是springboot方式, 那麽需要在啟動類上添加 @EnableAspectJAutoProxy 開啟AspectJ的支持

package com.wenbronk.aop;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 配置類, 省卻Application.java
 * Created by wenbronk on 2017/5/13.
 */
@SpringBootConfiguration
@ComponentScan(basePackages = {"com.wenbronk.aop"})
@EnableAspectJAutoProxy         // 開啟對AspectJ的支持
public class AopConfig {


}

6, 測試

package com.wenbronk.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 測試類
 * Created by wenbronk on 2017/5/13.
 */
public class TestAop {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);

        // 註解式攔截
        InterceptAnnotation annotation = context.getBean(InterceptAnnotation.class);
        annotation.intercept();

        // 方法攔截
        IntercepterMethod method = context.getBean(IntercepterMethod.class);
        method.intercepter();

        context.close();

    }

}

http://www.cnblogs.com/wenbronk/

springboot-3-aop