@Configuration proxyBeanMethods¶
The proxyBeanMethods attribute is a parameter found in the Spring Framework's @Configuration annotation.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md]
Function¶
This attribute controls whether the @Configuration class acts as a proxy for bean methods.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] When enabled, Spring uses a proxy to intercept calls to @Bean methods to ensure bean scoping behavior is respected (typically singletons).
Modes¶
The attribute accepts a boolean value with two distinct modes:
- Full Mode (
proxyBeanMethods = true): In this mode, Spring ensures that every@Beanmethod is intercepted.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] Consequently, regardless of how many times a method is called within the configuration class, the returned component is the same single instance.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] This is the default behavior and must be used when one component depends on another component defined within the same configuration class.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] - Lite Mode (
proxyBeanMethods = false): In this mode, Spring does not create a proxy for the configuration class.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] Each time an@Beanmethod is called, a new instance of the component is created.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] This mode serves as a optimization for configurations where there are no inter-bean dependencies.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md]
Best Practices¶
For scenarios where components defined in the configuration depend on one another, the Full mode (true) is required.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md] In the absence of such dependencies, the Lite mode (false) is typically the default preference for efficiency.^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md]
Related Concepts¶
- Spring Framework
- [[Bean Scopes]]
- [[Dependency Injection]]
Sources¶
^[600-developer__spring__尚矽谷-SpringBoot2核心技術.md]