spring-aop

aop的目标:

  1. 方法增强,构造器增强,属性增强
  2. 动态的向”对象“添加方法(Introduction)

核心概念:

  1. 技术本质:运行时的"动态字节码生成",使用JDK的Proxy和CGLIB技术
  2. 目前可以完成两大功能:方法调用代理和方法引入。
  3. 基本名词:Aspect,Advice,JoinPoint,Pointcut,Introduction,Weaving
  4. weaving的基本方式 :编译期(AspectJ),类加载期(AspectJ5),运行期(Spring)

Spring对AOP的支持:

  1. 经典方式(不推荐):
    基于org.aopalliance.aop.Advice 及ProxyFactoryBean,需要大量的配置及非pojo类的侵入式设计
  2. pojo 切面
    基于<aop: >方式,不需要引入接口,但只能基于xml方式
  3. 使用AspectJ的注解(需要引入AspectJweaver包)
    该方法只是使用了AspectJ的注解,来完成pojo切面的注解方式
  4. 使用AspectJ的切面
    利用强大的AspectJ框架的切面,完成构造器及属性拦截。

Aspect、Advice、Pointcut的定义,设定Joinpoint及Weaving

  1. 类级别上@Aspect
  2. 方法级别定义Joinpoint @Bfore,@After,@AfterThrowing,@AfterReturning,@Around
  3. 在joinpoint中,定义pointcut: @Before("execution(* package.ClassName.method(args))")
  4. (选项:pointcut的复用):
    @Pointcut("execution(* ...)") method(){};
    此时,@Before("method()")
  5. 注入容器:@Component或<bean class="..">
  6. 启动自动代理:@EnableAspectJAutoProxy或<aop:asepect-autoproxy>

处理目标中的参数

  1. 使用环绕通知中的参数获取 ProceedingJoinPoint
  2. 利用AspectJ指示器:args()及this()
  3. 使用:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {
    //单独定义pointcut的两种方式
    @Pointcut(value = "execution(* cn.johnyu.service.WorkService.work(int))&&args(arg)&&this(target)")
    public void pointcut1(int arg,Object target){};
    public final String pointcut2="execution(* cn.johnyu.service.WorkService.work(int))&&args(arg)&&this(target)";

    //定义Joinpoint及使用pointcut
    @Before("pointcut1(arg,target)")
    //@Before(pointcut2)
   // @Before(value = "execution(* cn.johnyu.service.WorkService.work(int))&&args(arg)&&this(target)")
    public void logBefore(int arg,Object target){
        //arg是target方法中的参数,target是当前被代理对象
    }
}

为“对象”加入新方法

  1. Java为静态语言,此项工作并不提供原生的支持

  2. 只能通过操作字节码的方案完成此项工作

  3. Spring利用AspectJ的注解,在运行期完成了此项工作。

  4. 新增加的接口及功能实现:

@Component
public class AttachFunctionImpl implements AttachFunction{
    @Override
    public void attach() {
        System.out.println("attach....");
    }
}
  1. 使用“Introductor”类
@Aspect
@Component
public class AttachIntroducer {
    // "+"表明,代理的目标为接口所有的实现类
    @DeclareParents(value = "cn.johnyu.service.WorkService+",defaultImpl = AttachFunctionImpl.class)
    public static AttachFunction attachFunction;
}
  1. 测试使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class AppTest {
    @Autowired private WorkService workService;
    @Test
    public void test1(){
        workService.work(2);
        AttachFunction a=(AttachFunction)workService;
        a.attach();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容