当前位置:网站首页>@Configuration (proxybeanmethods = false) what's the use of setting this to false
@Configuration (proxybeanmethods = false) what's the use of setting this to false
2022-07-29 03:38:00 【Gusu Leng】
Recently Nacos Source code , Find many Configuration On the configuration class @Configuration(proxyBeanMethods = false) All the proxyBeanMethods Set up a false 了 . Special research .

The default in the source code is true, The explanation of this attribute can also be roughly known .
1: If true, It means being @Bean The method of identification will be CGLIB Acting as agent , And will go bean Some behaviors in the life cycle of ( such as :@PostConstruct,@Destroy etc. spring The life cycle provided in ), If bean Is a singleton , So in the same configuration Call in
@Bean Method of identification , No matter how many times you call, you get the same bean, That is to say bean Initialize only once .
2: If false, Then the identification is @Bean Method of identification , Will not be intercepted for CGLIB agent , And I won't go bean Some behaviors in the life cycle of ( such as :@PostConstruct,@Destroy etc. spring The life cycle provided in ), If the same configuration Call in @Bean Method of identification , It's just the execution of ordinary methods , Does not get objects from the container . So if you call @Bean The identified method is a normal method call , And don't go bean Life cycle of .
therefore , If... In the configuration class @Bean If there is no dependent call between the identified methods , It can be set to false, It can avoid proxy operation by intercepting methods , It is also an optimization to improve performance . But we need to pay attention ,@Bean The value of the object will be put into the container , Get from container bean It can still be a single case , Will go through the life cycle .
See the following example :
public class MyBean {
@PostConstruct
public void init(){
System.out.println("MyBean Initialize the ");
}
public void hello(){
System.out.println("Mybean hello");
}
}public class YourBean {
public MyBean myBean;
public YourBean(MyBean myBean){
this.myBean=myBean;
}
@PostConstruct
public void init(){
System.out.println("YourBean Initialize the ");
}
public void hello(){
System.out.println("YourBean hello");
}
}@Configuration(proxyBeanMethods=true)
@Configuration(proxyBeanMethods = true)
public class ConfigureTest {
@Bean
public OrderEntity getOrderEntity(){
return new OrderEntity();
}
@Bean
public MyBean myBean(){
return new MyBean();
}
@Bean
public YourBean yourBean(){
return new YourBean(myBean());
}
}The test method :
@Component
public class InitClass implements InitializingBean , ApplicationContextAware {
ApplicationContext applicationContext;
@Override
public void afterPropertiesSet() throws Exception {
YourBean bean = this.applicationContext.getBean(YourBean.class); // first line
YourBean bean1 = this.applicationContext.getBean(YourBean.class); // The second line
MyBean myBean = this.applicationContext.getBean(MyBean.class); // The third line
myBean.hello();
bean.hello();
bean1.hello();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
}The first line is finished :MyBean and YourBean Are initialized , Description in new YourBean(myBean()) in myBean() go bean Life cycle of , Description by CGLIB Acting .

second , Three lines are finished , There is nothing to output , explain YourBean,MyBean All initialized once .
@Configuration(proxyBeanMethods=false)
@Configuration(proxyBeanMethods = false)
public class ConfigureTest {
@Bean
public OrderEntity getOrderEntity(){
return new OrderEntity();
}
@Bean
public MyBean myBean(){
return new MyBean();
}
@Bean
public YourBean yourBean(){
return new YourBean(myBean());
}
}Finish the first line , Only YourBean Initialization has output : See what's inside MyBean The attribute is @5351

Finish the second line , No content output ,bean1 Medium MyBean Properties are also @5351

The third line is finished ,MyBean Only after the initialization of , And this from the container bean yes @5360

See here should be able to experience ,proxyBeanMethods What do you mean . by false When ,@Bean Marked method calls are ordinary method calls , Will not be represented .
边栏推荐
- Easy to use remote sensing data set download website~~~
- AI_ Drug: VAE of molecular generation model (I)
- LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路
- 向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
- Notes on letter symbol marking of papers
- (2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)
- Example analysis of while, repeat and loop loops in MySQL process control
- What you see and think in Microsoft
- Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding
- Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
猜你喜欢

Casbin入门

Tonight at 7:30 | is the AI world in the eyes of Lianjie, Jiangmen, Baidu and country garden venture capital continue to be advanced or return to the essence of business

Asynchronous callback future mode of concurrent mode

Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules

VISO fast rendering convolution block

Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc

Flutter 启动白屏

Deep into C language (3) -- input and output stream of C
![MOS管 —— 快速复苏应用笔记(贰)[参数与应用]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]

深入C语言(3)—— C的输入输出流
随机推荐
Web uploader cannot upload multiple files
NXP i.mx8mp-deepviewrt
深入C语言(1)——操作符与表达式
Target detection, industrial defects, image segmentation -- deep learning data set induction
What you see and think in Microsoft
Naive Bayes -- continuous data
AI_ Drug: VAE of molecular generation model (I)
Idea configuration web container and war packaging
今晚7:30 | 连界、将门、百度、碧桂园创投四位大佬眼中的AI世界,是继续高深还是回归商业本质?...
Reproduce 20 character short domain name bypass and XSS related knowledge points
Division of data link layer, protocols used in data link layer and detailed introduction
CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.
MOS tube - rapid recovery application notes (II) [parameters and applications]
Swing V2: towards a larger model with larger capacity and higher resolution
3.2 model saving and loading
Deep into C language (1) -- operators and expressions
机器学习【Numpy】
再学EXKMP(EXKMP模板)
Arm architecture and neural network
RTP send and receive h265