当前位置:网站首页>@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
边栏推荐
- MySQL learning notes - data type (2)
- [book club issue 13] coding format of video files
- 2022年九大CIO趨勢和優先事項
- Width and alignment
- error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
- js平铺数据查找叶子节点
- Understand the context in go language in an article
- Actual combat | use composite material 3 in application
- Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
- 165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
猜你喜欢
Actual combat | use composite material 3 in application
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
QT graphical view frame: element movement
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
What is the future of the booming intelligent Internet of things (aiot) in recent years?
[hcie TAC] question 5 - 1
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
AI system content recommendation issue 24
Unity animation day05
Unity脚本生命周期 Day02
随机推荐
MySQL - MySQL adds self incrementing IDs to existing data tables
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
web聊天室实现
Enter the width!
Unity update process_ Principle of unity synergy
Proxifier global agent software, which provides cross platform port forwarding and agent functions
Function test - knowledge points and common interview questions
Go deep into the details of deconstruction and assignment of several data types in JS
Logstash~Logstash配置(logstash.yml)详解
Interpretation of the champion scheme of CVPR 2020 night target detection challenge
[hcie TAC] question 5 - 1
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
MySQL learning notes - data type (numeric type)
函数式接口,方法引用,Lambda实现的List集合排序小工具
这几年爆火的智能物联网(AIoT),到底前景如何?
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Unity脚本API—GameObject游戏对象、Object 对象
Unity script API - GameObject game object, object object
Redis sentinel mode realizes one master, two slave and three Sentinels
Selenium element interaction