springboot注解@Configuration属性proxyBeanMethods详解

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

1.看下源码注解
	/**
	 * 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来提高性能;

GitHub地址:https://github.com/mingyang66/spring-parent/tree/master/doc/base

  • 17
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
@Configuration注解是Spring框架中的一个注解,用于标识一个类是配置类。配置类是用来定义和组织Bean的创建和配置的类。在@Configuration注解的类中,可以使用@Bean注解来定义Bean的创建方法。配置类中的@Bean方法会在容器启动时被调用,并将返回的对象注册为Bean供其他组件使用。 在引用中的示例代码中,@Configuration注解被用于标识MyConfig类为一个配置类,并通过value属性设置了配置类的名称为"lixlConfig"。这样,在其他地方引用该配置类时可以使用这个名称来指定。 在引用中的示例代码中,通过使用Spring Boot的启动器类MainApplication,可以获取到容器中所有Bean的名称。这样可以验证配置类是否被正确加载,并且可以查看配置类的名称是否与预期一致。 在引用中的示例代码中,使用@Configuration注解的MyConfig类的value属性设置为"lixlConfig",并将proxyBeanMethods属性设置为false。该属性用于控制是否开启代理Bean方法的功能。当该属性为true时(默认值),Spring容器会根据@Bean方法的返回类型生成一个代理对象,用于控制方法的调用。当该属性为false时,Spring容器不会生成代理对象。 总结起来,@Configuration注解用于标识一个类是配置类,并通过其value属性设置配置类的名称。配置类中可以使用@Bean注解定义Bean的创建方法。@Configuration注解还可以通过proxyBeanMethods属性控制是否开启代理Bean方法的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot @Configuration注解](https://blog.csdn.net/weixin_38879931/article/details/122899017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [springboot @Configuration 注解详解](https://blog.csdn.net/nruuu/article/details/126798703)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值