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();
}

    未完待续!!!

最新文章

  1. 【学】jQuery的源码思路5——增加class的操作
  2. 转:JQuery.Ajax之错误调试帮助信息
  3. egit - not authorized
  4. ASP.NET MVC 使用 IOC框架 AutoFac 自动释放数据库资源
  5. CSS3动画制作的简单示例
  6. FolderBrowserDialog(文件夹浏览对话框)
  7. 通过python为zabbix发送告警邮件
  8. STM32串口控制步进电机(原创)
  9. Tabhost中Activity绑定Service
  10. DAY4-打卡第四天-2018-1-12
  11. 【python进阶】Garbage collection垃圾回收1
  12. Hadoop 的 TotalOrderPartitioner
  13. EF使用Fluent API配置映射关系
  14. Sql Server数据库之事务,视图,索引
  15. AI 判别式模型和生成式模型
  16. CentOS 7环境下Pycharm安装流程记录
  17. POJ 3295 Tautology 构造 难度:1
  18. 【科普】Web(瓦片)地图的工作原理
  19. 关于微信小程序登录,后端如何生成3rd_session?(后端为c#)
  20. 前端获取table表格里面的所有(单个)tr和所有(单个)td,用js实现

热门文章

  1. vue 运行脚手架报错
  2. 消费端ACK和重回队列
  3. Latex的beamer幻灯片图形不编号的问题
  4. POJ2942Knights of the round table
  5. C++入门经典-例9.1-函数模板,函数模板的作用,使用数组作为模板参数
  6. mysql 日常操作-DDL
  7. npm转成别的下载地址的插件
  8. python 购物车+用户认证程序
  9. list元素按照日期排序
  10. Java静态函数、父类、子类执行顺序