当前位置:网站首页>@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
边栏推荐
- Object distance measurement of stereo vision
- Solve the error of JSON module in PHP compilation and installation under CentOS 6.3
- 数据湖治理:优势、挑战和入门
- Decimal, exponential
- Unity脚本介绍 Day01
- Hidden communication tunnel technology: intranet penetration tool NPS
- LeetCode 1184. 公交站间的距离 ---vector顺逆时针
- [flask] ORM one to many relationship
- After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
- JS tile data lookup leaf node
猜你喜欢
Big God explains open source buff gain strategy live broadcast
一篇文章搞懂Go语言中的Context
Redis哨兵模式实现一主二从三哨兵
AutoCAD - set color
Case sharing | integrated construction of data operation and maintenance in the financial industry
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
2022年九大CIO趨勢和優先事項
QT graphical view frame: element movement
[native JS] optimized text rotation effect
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
随机推荐
web聊天室实现
Object distance measurement of stereo vision
The four most common errors when using pytorch
PR FAQ: how to set PR vertical screen sequence?
Essential basic knowledge of digital image processing
Implementation of web chat room
MySQL学习笔记——数据类型(2)
MYSQL索引优化
Unity动画Animation Day05
Book of night sky 53 "stone soup" of Apache open source community
JS to realize the countdown function
Scientific research cartoon | what else to do after connecting with the subjects?
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Neuf tendances et priorités du DPI en 2022
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Will the memory of ParticleSystem be affected by maxparticles
压力、焦虑还是抑郁? 正确诊断再治疗
Digital recognition system based on OpenCV
The per capita savings of major cities in China have been released. Have you reached the standard?