为了账号安全,请及时绑定邮箱和手机立即绑定

说说在 Spring 中如何使用 AspectJ 来实现 AOP

标签:
Java

AspectJ  是通过注解来描述切点与增强的。

1 开发环境要求

  • 因为要使用注解,所以请确保使用的 Java5.0 及以上版本。

  • 引入 AspectJ 相关类库:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version></dependency><dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${aspectj.version}</version></dependency><dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjtools</artifactId>
    <version>${aspectj.version}</version></dependency><dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>${aopalliance.version}</version></dependency>

2 编程方式

@Aspect//标识切面public class PreRentAspect {    /**
     * 增强逻辑
     */
    @Before("execution(* rent(..))")//定义切点与增强类型
    public void beforeRent() {
        System.out.println("开始执行租赁动作");
    }
}

这个切面只是一个普通的 POJO,只不过加了 @Aspect 注解。

@Before("execution(* rent(..))") 中的 @Before 表示增强类型是前置增强,它的内容是 @AspectJ 切点表达式,这里表示的是在目标类的 rent() 方法上织入增强, rent() 可以包含任意入参和任意的返回值。

带  @Aspect 的类,通过注解与代码,将切点、增强类型和增强的横切逻辑整合到了一起,是不是很方便呀O(∩_∩)O哈哈~

单元测试:

AspectJProxyFactory factory = new AspectJProxyFactory();//设置目标类factory.setTarget(new User());//添加切面类factory.addAspect(PreRentAspect.class);

User proxy = factory.getProxy();String userId = "001";
proxy.rent(userId);
proxy.back(userId);

输出结果:

--开始执行租赁动作--
User:租赁【充电宝】
User:归还【充电宝】

3 配置方式

<!-- 目标类--><bean id="user" class="net.deniro.spring4.aspectj.User"/><!-- 切面类--><bean class="net.deniro.spring4.aspectj.PreRentAspect"/><!-- 自动创建代理--><bean
        class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>

单元测试:

ApplicationContext context = new ClassPathXmlApplicationContext(spring.xml");
User user = (User) context.getBean("user");
String userId = "001";
user.rent(userId);
user.back(userId);

输出结果与编程方式完全相同。

也可以基于 Schema 的 aop 命名空间进行配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--aspectj 驱动器 -->
    <aop:aspectj-autoproxy/>

    <!-- 目标类-->
    <bean id="user" class="net.deniro.spring4.aspectj.User"/>


    <!-- 切面类-->
    <bean class="net.deniro.spring4.aspectj.PreRentAspect"/></beans>

这样的配置更加简洁。其实在 <aop:aspectj-atuoproxy/> 内部已经采用了自动代理模式啦 O(∩_∩)O哈哈~

<aop:aspectj-atuoproxy/>proxy-target-class 属性,默认为 false ,表示使用 JDK 动态代理技术织入增强;此值为 true 则表示使用 CGLib 动态代理技术织入增强 。   如果目标类没有声明接口,那么即使  proxy-target-class 设置为 false,也会自动使用 CGLib 动态代理织入增强的哟O(∩_∩)O哈哈~


基于 Java5.0+ 的项目,建议使用  AspectJ  来配置切点与增强,因为这样更简洁、也更直接。



作者:deniro
链接:https://www.jianshu.com/p/abf28c0a15b0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。


点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消