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.spring work.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
单元测试:
ApplicationContext context = new ClassPath ApplicationContext(spring. ");
User user = (User) context.getBean("user");
String userId = "001";
user.rent(userId);
user.back(userId);
输出结果与编程方式完全相同。
也可以基于 Schema 的 aop 命名空间进行配置:
<? version="1.0" encoding="UTF-8"?> <beans ns="http://www.spring work.org/schema/beans" ns:xsi="http://www.w3.org/2001/ Schema-instance" ns:aop="http://www.spring work.org/schema/aop" xsi:schemaLocation="http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans-4.0.xsd http://www.spring work.org/schema/aop http://www.spring work.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 来配置切点与增强,因为这样更简洁、也更直接。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
Android实现购物车添加商品特效
谷歌已推送 Android Q Beta 1
-
分析称华为鸿蒙 2020 年将成为全球第五大操作系统,占领 2% 市场份额
2026-05-15栏目: 教程
-
系统桌面图标个性化设置,Windows 7桌面如何隐藏控制面板图标
2026-05-15栏目: 教程
-
《吸血鬼:避世血族2》宣布延期至2020年末
2026-05-15栏目: 教程
-
微博:用户反馈张雨晗帐号存在刷数据行为,已暂停商业接单功能
2026-05-15栏目: 教程
-
常用手机APP修图软件,快捷又方便!
2026-05-15栏目: 教程
