`
v5qqbrowser
  • 浏览: 355056 次
文章分类
社区版块
存档分类
最新评论

AOP 通过aspectj实现引入通知

 
阅读更多

aspectj是Spring提供的可以通过代码的注释自动生成XML配置的包,这样可以简化代码使配置文件变得更为简单,但也无法机动的修改XML文件了,而且由于将代理的配置交给了aspectj处理,所以也无法看到SpringAOP代理的配置信息。但对于提高开发效率还是很有价值的。

如下两个接口:

TargetInterface是原始的对象接口而AdviceInterface是我们需要引入的借口。DefTarget和DefAdvice是上两个接口的实现。

  1. packageaspectTest;
  2. publicinterfaceTargetInterface{
  3. publicvoidfunctionA();
  4. publicintfunctionB();
  5. }

  1. packageaspectTest;
  2. publicinterfaceAdviceInterface{
  3. publicvoidfunctionC();
  4. publicintfunctionD();
  5. }

  1. packageaspectTest;
  2. publicclassDefTargetimplementsTargetInterface{
  3. publicvoidfunctionA(){
  4. System.out.println("functionA...");
  5. }
  6. publicintfunctionB(){
  7. System.out.println("functionB...");
  8. return0;
  9. }
  10. }
  1. packageaspectTest;
  2. publicclassDefTargetimplementsTargetInterface{
  3. publicvoidfunctionA(){
  4. System.out.println("functionA...");
  5. }
  6. publicintfunctionB(){
  7. System.out.println("functionB...");
  8. return0;
  9. }
  10. }

TargetAspect是Aspect实现代理的注释类,Aspect会访问其中的注释去配置AOP代理

  1. packageaspectTest;
  2. importorg.aspectj.lang.annotation.Aspect;
  3. importorg.aspectj.lang.annotation.DeclareParents;
  4. importorg.aspectj.lang.annotation.Pointcut;
  5. importaop.CallTracker;
  6. importaop.DefaultCallTracker;
  7. @Aspect
  8. publicclassTargetAspect{
  9. //方法名无所谓,Aspect之会寻找注释名
  10. //定义切入点
  11. @Pointcut("execution(*aspectTest.*.*(..))")
  12. privatevoidsetterServiceCall(){}
  13. //定义需要实现的对象(接口)
  14. @DeclareParents(
  15. value="aspectTest.*",
  16. defaultImpl=DefAdvice.class)//接口的实现类
  17. publicstaticAdviceInterfacemixin;
  18. }

Spring的配置文件,比较简单了

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop.xsd">
  10. <beanid="target"class="aspectTest.DefTarget"/>
  11. <beanclass="aspectTest.TargetAspect"/>
  12. <aop:aspectj-autoproxy/>
  13. </beans>

Main:

  1. packageaspectTest;
  2. importorg.springframework.context.ApplicationContext;
  3. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  4. importservices.StockService;
  5. importservices.UserService;
  6. importjava.math.BigDecimal;
  7. importjava.util.Date;
  8. publicclassMain{
  9. publicstaticvoidmain(String[]args){
  10. ApplicationContextac=newClassPathXmlApplicationContext(
  11. "aspectTest/context.xml"
  12. );
  13. TargetInterfaceti=(TargetInterface)ac.getBean("target");
  14. ti.functionA();
  15. ti.functionB();
  16. describeTracker(ti);
  17. }
  18. privatestaticvoiddescribeTracker(Objecto){
  19. AdviceInterfacet=(AdviceInterface)o;
  20. t.functionC();
  21. t.functionD();
  22. }
  23. }
分享到:
评论

相关推荐

    SpringBoot中的AOP+自定义注解(源代码)

    1.4 Spring AOP 和 AspectJ AOP 有什么区别? 2. 在 SpringBoot 中使用 Aop 功能 2.0 创建一个SpringBoot项目 2.1 引入 POM 依赖 2.1.1 引入springboot aop依赖 2.1.2 引入fastjson依赖 2.2 .编写配置类SpringConfig...

    spring源码详解

    一、什么是AOP 二、AOP相关概念 (1)切面 (Aspect) 交叉业务,也就是通用的...三、AOP实现方式 依据织入方式的不同,分为 1、编译期织入 2、类加载器织入 3、动态代理织入 AspectJ:1,2 Spring AOP:3

    Spring-Reference_zh_CN(Spring中文参考手册)

    引入通知 7.4. Spring里的advisor(Advisor) API 7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用...

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring API

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.2. 使用元数据驱动的自动代理 7.10. 使用TargetSource 7.10.1. 热交换...

    Spring 2.0 开发参考手册

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...

    spring chm文档

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.2. 使用元数据驱动的自动代理 7.10. 使用TargetSources 7.10.1. 热交换...

    Spring in Action(第2版)中文版

    1.4应用aop 1.4.1aop介绍 1.4.2aop使用 1.5小结 第2章基本bean装配 2.1容纳你的bean 2.1.1beanfactory介绍 2.1.2使用应用上下文 2.1.3bean的生命 2.2创建bean 2.2.1声明一个简单的bean 2.2.2通过构造函数...

    Spring in Action(第二版 中文高清版).part2

    1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP使用 1.5 小结 第2章 基本Bean装配 2.1 容纳你的Bean 2.1.1 BeanFactory介绍 2.1.2 使用应用上下文 2.1.3 Bean的生命 2.2 创建Bean 2.2.1 声明一个简单的Bean ...

    Spring in Action(第二版 中文高清版).part1

    1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP使用 1.5 小结 第2章 基本Bean装配 2.1 容纳你的Bean 2.1.1 BeanFactory介绍 2.1.2 使用应用上下文 2.1.3 Bean的生命 2.2 创建Bean 2.2.1 声明一个简单的Bean ...

    Spring攻略(第二版 中文高清版).part2

    第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...

    Spring攻略(第二版 中文高清版).part1

    第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...

    spring学习笔记

    Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................

Global site tag (gtag.js) - Google Analytics