springboot-3-aop

aop存在的目的是进一步解耦, spring支持aspectJ的注解式切面编程

1), 使用@Aspect声明为一个切面, 并使用@Component加入context中

2), 使用@After, @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() {};

    /**
     * 注解式拦截, 可在@After, @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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值