当前位置: 首页 > news >正文

济南建站方案赣州做网站建设

济南建站方案,赣州做网站建设,交通建设集团网站,文化传播公司网站模板Configuration注释中的proxyBeanMethods参数是springboot1.0,升级到springboot2.0之后新增的比较重要的内容,该参数是用来代理bean的。 理论 首先引出两个概念:Full 全模式,Lite 轻量级模式 Full(proxyBeanMethods true) :pro…

@Configuration注释中的proxyBeanMethods参数是springboot1.0,升级到springboot2.0之后新增的比较重要的内容,该参数是用来代理bean的。

理论

首先引出两个概念:Full 全模式,Lite 轻量级模式

Full(proxyBeanMethods = true) :proxyBeanMethods参数设置为true时即为:Full 全模式。 该模式下注入容器中的同一个组件无论被取出多少次都是同一个bean实例,即单实例对象,在该模式下SpringBoot每次启动都会判断检查容器中是否存在该组件
Lite(proxyBeanMethods = false) :proxyBeanMethods参数设置为false时即为:Lite 轻量级模式。该模式下注入容器中的同一个组件无论被取出多少次都是不同的bean实例,即多实例对象,在该模式下SpringBoot每次启动会跳过检查容器中是否存在该组件
什么时候用Full全模式,什么时候用Lite轻量级模式?
当在你的同一个Configuration配置类中,注入到容器中的bean实例之间有依赖关系时,建议使用Full全模式
当在你的同一个Configuration配置类中,注入到容器中的bean实例之间没有依赖关系时,建议使用Lite轻量级模式,以提高springboot的启动速度和性能

proxyBeanMethods 属性默认值是 true, 也就是说该配置类会被代理(CGLIB),在同一个配置文件中调用其它被 @Bean 注解标注的方法获取对象时会直接从 IOC 容器之中获取; 

 源码注解

    /**
     * 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;
 

注解的意思是 proxyBeanMethods 配置类是用来指定 @Bean 注解标注的方法是否使用代理,默认是 true 使用代理,直接从 IOC 容器之中取得对象;如果设置为 false, 也就是不使用注解,每次调用 @Bean 标注的方法获取到的对象和 IOC 容器中的都不一样,是一个新的对象,所以我们可以将此属性设置为 false 来提高性能;

根据注释 proxyBeanMethods 是为了让使用 @Bean 注解的方法被代理而实现 bean 的生命周期的行为。

  1. 设置为 true,那么直接调用方法获取 bean,不会创建新的 bean,而是会走 bean 的生命周期的行为。
  2. 设置为 false, 那么直接调用方法获取 bean,会创建新的 bean,且不会走 bean 的生命周期的行为。

测试代码


public class AnnotationConfigApplicationContextTest {@Testpublic void refresh(){AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext();ctx.register(AppConfig.class);ctx.refresh();MyBean myBean=ctx.getBean(MyBean.class);myBean.sayHello();YourBean yourBean=ctx.getBean(YourBean.class);yourBean.sayHello();}

Configuration 代码,设置为 false

@Configuration(proxyBeanMethods = false)
public class AppConfig {@Beanpublic MyBean myBean(){return new MyBean();}@Beanpublic YourBean yourBean(){return new YourBean(myBean());}
}

Mybean 代码

public class MyBean {public MyBean(){System.out.println("MyBean construct......");}@PostConstructpublic void init(){System.out.println("MyBean PostConstruct ....");}public void sayHello(){System.out.println("Hi MyBean ,hello world!");}
}

YourBean 代码

public class YourBean {public MyBean myBean;public YourBean(MyBean myBean){System.out.println("YourBean construct...");this.myBean=myBean;}@PostConstructpublic void init(){System.out.println("YourBean PostConstruct...");}public void sayHello(){System.out.println("Hi YourBean ,hello world!");}
}

执行 refresh 测试测试代码结果(proxyBeanMethods = false)

MyBean construct...... 打印了两次,说明被 new 了两次

10:04:56.066 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBean'
MyBean construct......
MyBean PostConstruct ....
10:04:56.069 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'yourBean'
MyBean construct......
YourBean construct...
YourBean PostConstruct...
Hi MyBean ,hello world!
Hi YourBean ,hello world!

执行 refresh 测试测试代码结果(proxyBeanMethods = true) 

MyBean construct...... 只打印了一次,说明只被 new 了一次。

10:06:51.727 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBean'
MyBean construct......
MyBean PostConstruct ....
10:06:51.741 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'yourBean'
YourBean construct...
YourBean PostConstruct...
Hi MyBean ,hello world!
Hi YourBean ,hello world!

 

http://www.yayakq.cn/news/42004/

相关文章:

  • 镇江市质监站网址wordpress网站换字体颜色
  • 北京市住房城乡建设部网站网站设计优点
  • 建设企业网站有哪些阳泉那有做网站的
  • 开个做网站的公司 知乎张家口网站建设工作室
  • 轮播网站合肥专业做网站的公司
  • 蚌埠北京网站建设手机平台网站开发
  • 购物商城网站开发镇海阿里巴巴关键词优化
  • 三明网站建设上海网站空间服务器
  • 大型网站系统图厦门logo设计公司
  • 深圳建设银行宝安支行网站电商创业项目有哪些
  • 海口网站建设呢安徽网新科技有限公司官网
  • 使用aspx做电影网站清河做网站哪儿好
  • 上海兼职做网站站外推广营销方案
  • 公文写作 课程中心网站建设营销策略分析论文
  • 南京医院网站建设做网站宣传
  • 的建站公司宣传片拍摄脚本模板
  • wordpress 仿neoeaae沈阳网站优化推广方案
  • 网站建设吗简洁大气企业网站源码 后台
  • 南宁网站托管4徐汇区网站建设
  • iis7 网站无法显示该页面做广告推广哪家好
  • jsp做网站都可以做什么哪里有好网站设计
  • 企业网站设计方案建设银行网站下载中心在哪
  • 公司的网站建设公司网站建设网站建设制作设计seo优化珠海
  • 绿茵足球网站建设政务服务 网站 建设方案
  • 汕头市小程序定制公司啥叫优化
  • 苏州网站排名优化报价北海网站制作公司
  • 做面包网站wordpress 事件插件
  • 网站开发先找UI吗静态网站如何共用一个头部和尾部
  • 深圳专业建站公司有哪些建筑工程网上申请质量安全监督
  • 网上做代卖的网站网站建设平ppt