简介
说明
本文介绍SpringBoot中@Configuration注解proxyBeanMethods的含义。
SpringBoot源码的写法
看过SpringBoot源码后发现,Spring的自动配置类都是这样写的:@Configuration(proxyBeanMethods = false)。
proxyBeanMethods的取值
proxyBeanMethods属性的默认值是true。
true
说明
如果为true,则是Full模式。被@Bean标识的方法会做如下处理:
- 都会被CGLIB进行代理
- 会走bean的生命周期中的一些行为(比如:@PostConstruct、@Destroy等 Spring中提供的生命周期)
- 无论调用几次,得到的都是同一个bean
优点
可以支持通过常规Java调用相同类的@Bean方法而保证是容器内的Bean,这有效规避了在“Lite模式”下操作时难以跟踪的细微错误。
缺点
- 启动时会给该配置类生成一个CGLIB子类放进容器(代理),会增加启动时间。
- Spring Boot有很多配置类,这个开销是不容忽视的。这也是Spring 5.2新增了proxyBeanMethods属性的原因
- 因为被代理了,所以@Bean方法 不可以是private、不可以是final
false
说明
如果为true,则是Lite模式。被@Bean标识的方法会做如下处理:
- 不会被拦截进行CGLIB代理
- 不会走bean的生命周期中的一些行为(比如:@PostConstruct、@Destroy等 Spring中提供的生命周期)
- 如果同一个Configuration中调用@Bean标识的方法,就只是普通方法的执行而已,不会从容器中获取对象。
- @Bean标识的返回值对象还是会放入到容器中的,从容器中获取bean还是可以是单例的,会走生命周期。
优点
- 启动时不需要给配置类生成CGLIB子类,减少了启动时间
- 可以将配置类当作一个普通类使用:也就是说@Bean方法 可以是private、可以是final
缺点
- 不能声明@Bean之间的依赖(也就是说不能通过方法调用来依赖其它Bean)
- 解决方案:把依赖Bean放进方法入参里
如何选用true/false
如果配置类中的@Bean标识的方法之间不存在依赖调用的话,可以设置为false,可以避免拦截方法进行代理操作,提升性能。
实例
Bean
package com.knife.tmp; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class MyUtil { private String name; public void welcome(String customer) { System.out.println("Welcome, " + customer); } }
配置类
package com.knife.tmp; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration(proxyBeanMethods = false) public class CommonConfig { @Bean public MyUtil myUtil1(){ System.out.println("myUtil1方法"); myUtil2(); return new MyUtil("Tony"); } @Bean public MyUtil myUtil2(){ System.out.println("myUtil2方法"); return new MyUtil("Peter"); } }
其他类
package com.knife.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextHolder implements ApplicationContextAware { private static ApplicationContext context; public void setApplicationContext(ApplicationContext context) throws BeansException { ApplicationContextHolder.context = context; } public static ApplicationContext getContext() { return context; } }
测试类
package com.knife.controller; import com.knife.util.ApplicationContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/test") public String test() { Object myUtil2 = ApplicationContextHolder.getContext().getBean("myUtil2"); return "test success"; } }
测试
测试1:proxyBeanMethods = false
启动结果:myUtil2方法执行了两次。

打个断点:

结果:

释放断点,再次访问:发现多次获取的myUtil2这个bean都是同一个

测试2:proxyBeanMethods = true
修改配置类为:
package com.knife.tmp; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration(proxyBeanMethods = true) public class CommonConfig { @Bean public MyUtil myUtil1(){ System.out.println("myUtil1方法"); myUtil2(); return new MyUtil("Tony"); } @Bean public MyUtil myUtil2(){ System.out.println("myUtil2方法"); return new MyUtil("Peter"); } }
启动结果:myUtil2方法执行了1次。

打个断点:

结果:

释放断点,再次访问:发现多次获取的myUtil2这个bean都是同一个

测试3:不指定proxyBeanMethods
启动结果:myUtil2方法执行了1次。

打个断点:

结果:

释放断点,再次访问:发现多次获取的myUtil2这个bean都是同一个

请先
!