当前位置:网站首页>@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
边栏推荐
- Introduction of text mining tools [easy to understand]
- Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
- Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
- Decimal, exponential
- Interpretation of the champion scheme of CVPR 2020 night target detection challenge
- 数据库函数的用法「建议收藏」
- CentOS 6.3 下 PHP编译安装JSON模块报错解决
- c# 实现定义一套中间SQL可以跨库执行的SQL语句
- Redis sentinel mode realizes one master, two slave and three Sentinels
- MySQL index optimization
猜你喜欢

MYSQL索引优化

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

Unity script lifecycle day02

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

科研漫画 | 联系到被试后还需要做什么?

Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?

The per capita savings of major cities in China have been released. Have you reached the standard?

What is the future of the booming intelligent Internet of things (aiot) in recent years?
Redis哨兵模式实现一主二从三哨兵

AI has surpassed Dr. CS in question making?
随机推荐
Align left and right!
.Net 应用考虑x64生成
What does IOT engineering learn and work for?
Unity script lifecycle day02
Feature extraction and detection 15-akaze local matching
Go deep into the details of deconstruction and assignment of several data types in JS
Unity脚本API—Transform 变换
Big God explains open source buff gain strategy live broadcast
Unity update process_ Principle of unity synergy
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
Selenium element interaction
Audio and video technology development weekly | 252
Nine CIO trends and priorities in 2022
Unity脚本常用API Day03
深入JS中几种数据类型的解构赋值细节
Huawei cloud database DDS products are deeply enabled
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
Width and alignment
中国主要城市人均存款出炉,你达标了吗?
The per capita savings of major cities in China have been released. Have you reached the standard?