当前位置:网站首页>@EnableAspectAutoJAutoProxy_ Exposeproxy property
@EnableAspectAutoJAutoProxy_ Exposeproxy property
2022-07-04 16:07:00 【cristianoxm】
One 、@EnableAspectAutoJAutoProxy Turn on AOP function
@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({
AspectJAutoProxyRegistrar.class})
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
- Attribute interpretation
- proxyTargetClass: Represents the implementation of dynamic proxy , If the value is set true, Indicates that proxy classes are based on CGLIB To achieve ; By default, the value is set to false Indicates that if the original class defines an interface, it passes JDK.Proxy Implementation is otherwise based on CGLIB To achieve .
- exposeProxy: exposeProxy=true Spring The current proxy object will be stored in ThreadLocal in
Two 、 About exposeProxy
- Let's look at an example
- Interface :
public interface IUserService {
boolean showName();
void showAge();
}
- Implementation interface
@Service
public class UserService implements IUserService {
@Override
public boolean showName() {
System.out.println("UserService.showName");
showAge();
return false;
}
@Override
public void showAge() {
System.out.println("UserService.showAge");
}
}
- section
@Component
@Aspect
public class MyAspect {
@Before("execution(* com.aop.UserService.*(..))")
@AfterReturning
public void myBefore(JoinPoint joinPoint) {
System.out.println("before invoke "+joinPoint.getSignature().getName());
}
- Config
@Configuration
@ComponentScan("com.aop")
@EnableAspectJAutoProxy
public class AppConfig {
}
- Test
public class TestAOP {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
IUserService userService = (IUserService) ctx.getBean("userService");
userService.showName();
}
}
- result
before invoke showName
UserService.showName
UserService.showAge
- reflection :
Why not output before invoke showAge - The reason is that :
public boolean showName() {
System.out.println("UserService.showName");
// The essence is this.showAge(), Here it is. UserService The original object of , Not by spring Strengthened Proxy Object to call
showAge();
return false;
}
- resolvent : The core is
Get Spring Enhanced proxy object , Call again.
- Give Way UserService Perceive ApplicationContextAware, Make it from beanFactory Get Proxy Object capability . Be careful
Out of commission new AnnotationConfigApplicationContext(), such Spring There will be two containers
@Service
public class UserService implements IUserService , ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public boolean showName(){
System.out.println("UserService.showName");
IUserService userService = (IUserService) applicationContext.getBean("userService");
userService.showAge();
return true;
}
@Override
public void showAge() {
System.out.println("UserService.showAge");
}
}
-----------------------------------------------------
result :
before invoke showName
UserService.showName
before invoke showAge
UserService.showAge
- stay ThreadLocal Get to the Proxy object , This is a Spring Capabilities provided , However, to enable this function, you need to set
exposeProxy=true:Store the current proxy object in ThreadLocal in
@Service
public class UserService implements IUserService {
@Override
public boolean showName() {
System.out.println("UserService.showName");
//TheadLocal in Get proxy object And call
IUserService userService = (IUserService) AopContext.currentProxy();
userService.showAge();
return true;
}
@Override
public void showAge() {
System.out.println("UserService.showAge");
}
}
If not :@EnableAspectJAutoProxy(exposeProxy = true), Report the following error :
- Source code :
stay Spring.JdkDynamicAopProxy.invoke() In the method , You can see the following :
if (this.advised.exposeProxy) {
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
3、 ... and 、Spring They don't advocate the above methods ,Spring The official document suggests , Don't call each other in proxy methods
边栏推荐
- 案例分享|金融业数据运营运维一体化建设
- Can I "reverse" a Boolean value- Can I 'invert' a bool?
- One question per day 540 A single element in an ordered array
- %F format character
- What is the catalog of SAP commerce cloud
- 2022年九大CIO趨勢和優先事項
- [native JS] optimized text rotation effect
- 科研漫画 | 联系到被试后还需要做什么?
- [North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
- 函数式接口,方法引用,Lambda实现的List集合排序小工具
猜你喜欢

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04

2022年九大CIO趨勢和優先事項

函数式接口,方法引用,Lambda实现的List集合排序小工具

MySQL learning notes - data type (2)
![[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection](/img/a7/92d670776e3fd3d5add3aa144617c7.jpg)
[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection

MySQL学习笔记——数据类型(数值类型)

MYSQL索引优化

MySQL learning notes - data type (numeric type)

Neuf tendances et priorités du DPI en 2022
随机推荐
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Unity script lifecycle day02
Intranet penetrating FRP: hidden communication tunnel technology
Unity animation day05
华为云数据库DDS产品深度赋能
Actual combat | use composite material 3 in application
Unity script API - transform transform
hexadecimal
函数式接口,方法引用,Lambda实现的List集合排序小工具
CentOS 6.3 下 PHP编译安装JSON模块报错解决
一篇文章学会GO语言中的变量
Unity script API - component component
Unity脚本API—Component组件
Redis sentinel mode realizes one master, two slave and three Sentinels
How can floating point numbers be compared with 0?
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
Working group and domain analysis of Intranet
Redis' optimistic lock and pessimistic lock for solving transaction conflicts