一、proxybeanMethods源码注释
/**
* Specify whether {@code @Bean} methods should get proxied in order to enforce
* bean lifecycle behavior, e.g. to return shared singleton bean instances even
* in case of direct {@code @Bean} method calls in user code. This feature
* requires method interception, implemented through a runtime-generated CGLIB
* subclass which comes with limitations such as the configuration class and
* its methods not being allowed to declare {@code final}.
* <p>The default is {@code true}, allowing for 'inter-bean references' via direct
* method calls within the configuration class as well as for external calls to
* this configuration's {@code @Bean} methods, e.g. from another configuration class.
* If this is not needed since each of this particular configuration's {@code @Bean}
* methods is self-contained and designed as a plain factory method for container use,
* switch this flag to {@code false} in order to avoid CGLIB subclass processing.
* <p>Turning off bean method interception effectively processes {@code @Bean}
* methods individually like when declared on non-{@code @Configuration} classes,
* a.k.a. "@Bean Lite Mode" (see {@link Bean @Bean's javadoc}). It is therefore
* behaviorally equivalent to removing the {@code @Configuration} stereotype.
* @since 5.2
*/
boolean proxyBeanMethods() default true;
指定是否@Bean方法应以执行bean的生命周期行为,例如,即使在直接的情况下返回共享单bean实例获得代理@Bean用户代码的方法调用。 该功能要求的方法的拦截,通过它配有限制的运行时生成的CGLIB子类实现如配置类及其方法不被允许声明final 。
默认为true ,允许通过配置类内部以及外部调用该配置的直接方法调用“bean间引用” @Bean方法,例如从另一个配置类。 如果这不是因为每个的这种特定配置的需要@Bean方法是自包含的并且设计为用于容器使用一个普通的工厂的方法,切换该标志来false以免CGLIB子类的处理。
关闭bean方法拦截有效地处理@Bean方法来单独像非申报时@Configuration类,又名“@Bean精简版模式”(见@Bean's javadoc )。 因此,它是行为上等同于除去@Configuration铅板。从5.2版版本开始。
二、简单来说
注解的意思是proxyBeanMethods配置类是用来指定@Bean注解标注的方法是否使用代理,
2.1 默认是true使用代理,直接从IOC容器之中取得对象;
package com.tree.securitydemo.config;
import lombok.extern.java.Log;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* proxybeanMethods
*
* @author zhong
* @create 2020-10-11 12:14
* @contact 1478168700@qq.com
**/
@Configuration(proxyBeanMethods = true)
@Log
public class ProxyBeanMethodsConfig {
@Bean
MyBean1 myBean1(){
MyBean1 myBean1 = new MyBean1();
//调用myBean2()方法
myBean1.setMyBean2(myBean2());
return myBean1;
}
@Bean
MyBean2 myBean2(){
log.info("myBean2()被调用啦");
MyBean2 myBean2 = new MyBean2();
log.info(myBean2.toString());
return myBean2;
}
}
class MyBean1{
private MyBean2 myBean2;
public MyBean2 getMyBean2() {
return myBean2;
}
public void setMyBean2(MyBean2 myBean2) {
this.myBean2 = myBean2;
}
}
class MyBean2{
public MyBean2(){
}
}
此时控制台只会调用一次myBean2P()方法
2.2 如果设置为false,也就是不使用注解,每次调用@Bean标注的方法获取到的对象和IOC容器中的都不一样,是一个新的对象,所以我们可以将此属性设置为false来提高性能。
package com.tree.securitydemo.config;
import lombok.extern.java.Log;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* proxybeanMethods
*
* @author zhong
* @create 2020-10-11 12:14
* @contact 1478168700@qq.com
**/
@Configuration(proxyBeanMethods = false)
@Log
public class ProxyBeanMethodsConfig {
@Bean
MyBean1 myBean1(){
MyBean1 myBean1 = new MyBean1();
//调用myBean2()方法
myBean1.setMyBean2(myBean2());
return myBean1;
}
@Bean
MyBean2 myBean2(){
log.info("myBean2()被调用啦");
MyBean2 myBean2 = new MyBean2();
log.info(myBean2.toString());
return myBean2;
}
}
class MyBean1{
private MyBean2 myBean2;
public MyBean2 getMyBean2() {
return myBean2;
}
public void setMyBean2(MyBean2 myBean2) {
this.myBean2 = myBean2;
}
}
class MyBean2{
public MyBean2(){
}
}
此时控制台调用了两次myBean2()