Spring(十二)--Spring AspectJ
2024-09-02 10:48:40
Spring AspectJ
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
AspectJ 是一个面向切面的框架!定义了AOP的语法!
Spring 将AspectJ 整合到了自己的框架中!
需要引入两个核心jar
01.aspectj.weaver
02.spring-aspects
(如果你使用的是idea 所需的所有pom节点,在这儿:https://www.cnblogs.com/fl72/p/9625697.html)
务必掌握的 AspectJ 的切入点表达式
execution([访问权限类型] 返回值类型 [完整限定类名]方法名 (参数) [抛出的异常类型])
execution( 返回值类型 方法名(参数))
*:0-N的字符
..:
01.如果是在方法参数中,表示参数可有多个或者可无
02.如果是在包名之后,表示当前包和子包
+:
01.如果定义在类后面,表示当前类以及子类
02.如果定义在接口后面,表示当前接口以及实现类
例子:
1. 使用注解实现
/**
* 当前类就是 整个程序中需要的各种系统级业务
* 就是一个切面类
*/
@Aspect
public class MyAspectJ { @Before("execution(* *..UserDao.sleep(..))")
public void before(){
System.out.println("前置增强........");
} @AfterReturning("execution(* *..UserDao.sleep(..))")
public void afterReturning(){
System.out.println("后置增强........");
}
//如果想获取方法的返回值
@AfterReturning(value = "execution(* *..UserDao.sleep(..))",returning = "result")
public void afterReturning(String result){
System.out.println("后置增强........"+result);
} /**
* 环绕增强可以改变返回值
*/
@Around("execution(* *..UserDao.eat(..))")
public Object around(ProceedingJoinPoint point){
System.out.println("环绕增强进来........");
Object result=null;
try {
result= point.proceed(); //执行目标方法
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕增强出去........");
return "orange";
}
}
2. 对应的xml文件和测试方法
<!--01.配置目标对象-->
<bean id="userDao" class="com.xdf.dao.UserDaoImpl"/> <!--02.配置切面-->
<bean id="myAspectJ" class="com.xdf.annotation.MyAspectJ"/> <!--03.注册aspectj的自动代理-->
<aop:aspectj-autoproxy/> //测试方法
@Test
public void defaultTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
UserDao dao= context.getBean("userDao", UserDao.class);
// System.out.println(dao.eat());
dao.sleep();
}
3. 使用纯切面的方式实现
public class MyAspectJ { public void before(){
System.out.println("前置增强........");
} public void afterReturning(){
System.out.println("后置增强........");
} /**
* 环绕增强可以改变返回值
*/
public Object around(ProceedingJoinPoint point){
System.out.println("环绕增强进来........");
Object result=null;
try {
result= point.proceed(); //执行目标方法
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕增强出去........");
return "orange";
} }
4.对应的xml文件和测试方法
<!--01.配置目标对象-->
<bean id="userDao" class="com.xdf.dao.UserDaoImpl"/> <!--02.配置切面-->
<bean id="myAspectJ" class="com.xdf.annotation.MyAspectJ"/> <!--03.注册aspectj需要的切入点-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="myPonit" expression="execution(* *..UserDao.sleep(..))"/>
<aop:pointcut id="myPonit2" expression="execution(* *..UserDao.eat(..))"/>
<!--配置切面-->
<aop:aspect ref="myAspectJ">
<aop:before method="before" pointcut-ref="myPonit"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPonit"/>
<aop:around method="around" pointcut-ref="myPonit2"/>
</aop:aspect>
</aop:config>
@Test
public void aspectJTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("aspectJ.xml");
UserDao dao= context.getBean("userDao", UserDao.class);
//System.out.println(dao.eat());
dao.sleep();
}
未完待续!!!
最新文章
- 【学】jQuery的源码思路5——增加class的操作
- 转:JQuery.Ajax之错误调试帮助信息
- egit - not authorized
- ASP.NET MVC 使用 IOC框架 AutoFac 自动释放数据库资源
- CSS3动画制作的简单示例
- FolderBrowserDialog(文件夹浏览对话框)
- 通过python为zabbix发送告警邮件
- STM32串口控制步进电机(原创)
- Tabhost中Activity绑定Service
- DAY4-打卡第四天-2018-1-12
- 【python进阶】Garbage	collection垃圾回收1
- Hadoop 的 TotalOrderPartitioner
- EF使用Fluent API配置映射关系
- Sql Server数据库之事务,视图,索引
- AI 判别式模型和生成式模型
- CentOS 7环境下Pycharm安装流程记录
- POJ 3295 Tautology 构造 难度:1
- 【科普】Web(瓦片)地图的工作原理
- 关于微信小程序登录,后端如何生成3rd_session?(后端为c#)
- 前端获取table表格里面的所有(单个)tr和所有(单个)td,用js实现