1. 手动代理 静态代理
每个类都需要手动配置一遍. 静态代理
【框架】[Spring]AOP拦截-三种方式实现自动代理
详见 " 原来不用自动代理的配置文件如下:"
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
}
还能通过继承实现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 > 风格大量使用了 Spring 的自动代理机制。如果您已经通过使用 BeanNameAutoProxyCreator 或类似的方法使用显式自动代理,那么这可能会导致问题(比如advice没有编织)。推荐的使用模式是只使用 < aop: config > 样式,或者只使用 AutoProxyCreator 样式。
2.5 代理顺序 order