spring aop 切面配置

1. 手动代理 静态代理 

   每个类都需要手动配置一遍. 静态代理

   【框架】[Spring]AOP拦截-三种方式实现自动代理_weixin_34252090的博客-CSDN博客

详见 " 原来不用自动代理的配置文件如下:"

2. 自动代理 

  2.1 三种自动代理

Spring-AOP 自动创建代理 总览

BeanNameAutoProxyCreator beanName 无法使用包名

DefaultAdvisorAutoProxyCreator 可以用包名

 使用了RegexpMethodPointcutAdvisor这个类.    除上面的case外,还有个case   自动创建代理-DefaultAdvisorAutoProxyCreator - 腾讯云开发者社区-腾讯云

AnnotationAwareAspectJAutoProxyCreator AspectJ注解

 2.2 概念

9. Aspect Oriented Programming with Spring - AOP concepts

   2.1.1 aspect

        切面

  2.1.2 pointCut

        切点

  2.1.3 advice

    建议, (细分为 Before advice, after advice,aroundAdvice) , 着重强调了 intercepter 是一种特俗的advice .MethodInterceptor也是实现了advice接口. 配置和其他advice一致. 例子 methodInterceptor

    详见 9. Aspect Oriented Programming with Spring - aop-schema-advice

    和   Section 10.3.2, “Advice types in Spring”

    

  2.1.4 advisors

  The concept of "advisors" is brought forward from the AOP support defined in Spring 1.2 and does not have a direct equivalent in AspectJ. An advisor is like a small self-contained aspect that has a single piece of advice. The advice itself is represented by a bean, and must implement one of the advice interfaces described in Section 10.3.2, “Advice types in Spring”. Advisors can take advantage of AspectJ pointcut expressions though.

 “ Advisors”的概念是从 Spring 1.2中定义的 AOP 支持中提出来的,在 AspectJ 中没有直接的等价物。Advisors就像一个小的自足的aspect,只有一条建议(advice)。通知(advice)本身由一个 bean 表示,必须实现10.3.2节“ Spring 中的通知(advice)类型”中描述的通知(advice)接口之一。不过,Advisors可以利用 AspectJ 切入点表达式。

2.3 BeanNameAutoProxyCreator模式

        BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
        transactionAutoProxy.setProxyTargetClass(true); 

 //  如果不设置,它使用仅支持接口的 JDK 代理. 面对无接口的类时会报错。详见BeanNameAutoProxyCreator setBeanNames 正则表达式不起作用? spring 代理原理 ,注解失效的坑以及解决方案._个人渣记录仅为自己搜索用的博客-CSDN博客 



        transactionAutoProxy.setBeanNames("acheng1314.cn.service.*ServiceImpl.*(..)");
        transactionAutoProxy.setInterceptorNames("transactionInterceptor");

<bean
            class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>xxxClient</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>beanMockInterceptor</value>
            </list>
        </property>
  </bean>
 

<bean id="beanMockInterceptor" class="com.xxx.BeanMockInterceptor"/>

其中 

BeanMockInterceptor implements MethodInterceptor {

}

还能通过继承实现bean黑名单的

2.4 aop:config模式

 2.4.1 Declaring an aspect

<aop:config>
  <aop:aspect id="myAspect" ref="aBean">
    ...
  </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
  ...
</bean>

2.4.2 Declaring a pointcut

<aop:config>

  <aop:pointcut id="businessService"
        expression="execution(* com.xyz.myapp.service.*.*(..))"/>

</aop:config>

详见 官网文档

9. Aspect Oriented Programming with Spring - Schema-based AOP support

9.3 Schema-based AOP support

The <aop:config> style of configuration makes heavy use of Spring's auto-proxying mechanism. This can cause issues (such as advice not being woven) if you are already using explicit auto-proxying via the use of BeanNameAutoProxyCreator or suchlike. The recommended usage pattern is to use either just the <aop:config> style, or just the AutoProxyCreator style.

翻译:  < aop: config > 风格大量使用了 Spring 的自动代理机制。如果您已经通过使用 BeanNameAutoProxyCreator 或类似的方法使用显式自动代理,那么这可能会导致问题(比如advice没有编织)。推荐的使用模式是只使用 < aop: config > 样式,或者只使用 AutoProxyCreator 样式。

2.5 代理顺序 order

spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序) - 反光的小鱼儿 - 博客园

一次异常定位, spring自动创建代理类DefaultAdvisorAutoProxyCreator和BeanNameAutoProxyCreator的声明顺序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值