当前位置:网站首页>@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
边栏推荐
- 科研漫画 | 联系到被试后还需要做什么?
- [North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
- Unity script API - GameObject game object, object object
- Blood cases caused by Lombok use
- Enter the width!
- Implementation of web chat room
- Scientific research cartoon | what else to do after connecting with the subjects?
- What is the catalog of SAP commerce cloud
- Logstash ~ detailed explanation of logstash configuration (logstash.yml)
- Lombok使用引发的血案
猜你喜欢
这几年爆火的智能物联网(AIoT),到底前景如何?
中国主要城市人均存款出炉,你达标了吗?
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Will the memory of ParticleSystem be affected by maxparticles
Lombok使用引发的血案
In today's highly integrated chips, most of them are CMOS devices
Scientific research cartoon | what else to do after connecting with the subjects?
华为云数据库DDS产品深度赋能
一篇文章搞懂Go语言中的Context
随机推荐
Lombok使用引发的血案
【读书会第十三期】 音频文件的封装格式和编码格式
%S format character
Actual combat | use composite material 3 in application
[book club issue 13] ffmpeg common methods for viewing media information and processing audio and video files
Unity脚本介绍 Day01
Shell programming basics
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
一篇文章搞懂Go语言中的Context
AutoCAD - set color
Common API day03 of unity script
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
Unity prefab day04
The per capita savings of major cities in China have been released. Have you reached the standard?
AI system content recommendation issue 24
Selenium element interaction
.Net 应用考虑x64生成
What is the catalog of SAP commerce cloud
Scientific research cartoon | what else to do after connecting with the subjects?