亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

說說在 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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消