[英]Prototype bean scope implementation in Spring Framework
Hello guys I am trying to understand how exactly prototype Scope works in Spring IoC.大家好,我想了解原型 Scope 在 Spring IoC 中的工作原理。
For prototype beans I tried to understand by:对于原型 bean,我试图通过以下方式理解:
1. Reading this again and again but could not understand fully( https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-scopes-other-injection ) 1.一遍又一遍地阅读但无法完全理解( https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-scopes-other-injection )
2. Tried to debug via source code,only understood one thing if we do not specify proxyMode, it will not create proxy for prototype bean. 2.尝试通过源码调试,只明白一件事,如果我们不指定proxyMode,它就不会为prototype bean创建代理。
3. default ScopedProxyMode
for prototype scope is DEFAULT which typically equals NO.. unless configured in component scan. 3. 原型 scope 的默认
ScopedProxyMode
为 DEFAULT,通常等于 NO.. 除非在组件扫描中配置。
@Bean
@Scope("prototype")
public Employee employee(){
return new Employee();
}
@Bean
@Scope("prototype")
public Employee employee(){
return new Employee();
}
@Bean
@Scope("singleton")
public Department department(){
return new Department();
}
@Bean
@Scope("prototype",proxyMode= ScopedProxyMode.TARGET_CLASS)
public Employee employee(){
return new Employee();
}
@Bean
@Scope("singleton")
public Department department(){
return new Department();
}
What I typically saw in implementation examples on internet is people often do not configure proxyMode either mistakenly or are not aware about this option or maybe they are right.我通常在互联网上的实现示例中看到的是,人们通常不会错误地配置 proxyMode,或者不知道这个选项,或者他们可能是对的。
Thank you for your valuable time.感谢您宝贵的时间。
The prototype scope instructs Spring to instantiate a new instance of the bean everytime a bean instance is requested by the application.原型 scope 指示 Spring 每次应用程序请求 bean 实例时实例化 bean 的新实例。
You should consider using prototype scope in the following scenarios:您应该考虑在以下情况下使用原型 scope:
It is a good practice that your beans should implement some interface. bean 应该实现一些接口是一个很好的做法。 In that case, Spring uses JDK proxy mechanism to create proxy of your class.
在这种情况下,Spring 使用 JDK 代理机制来创建 class 的代理。 But implementing interface is not a requirement.
但是实现接口不是必需的。 If no interface is implemented by your bean, then Spring uses CGLIB library to create proxy of your bean (unless a class is annotated with @Configuration in which case a different proxy mechanism is used).
如果您的 bean 没有实现任何接口,则 Spring 使用 CGLIB 库来创建您的 bean 的代理(除非 class 使用 @Configuration 注释,在这种情况下使用不同的代理机制)。
By specifying proxyMode=ScopedProxyMode.TARGET_CLASS
, you are forcing Spring to use CGLIB proxy mechanism.通过指定
proxyMode=ScopedProxyMode.TARGET_CLASS
,您将强制 Spring 使用 CGLIB 代理机制。 In the end, just providing @Scope("prototype")
is sufficient.最后,只需提供
@Scope("prototype")
就足够了。
On a side note, know that if you have any methods that hook into lifecycle of a bean (like @PreDestroy
, implementing DisposableBean
interface, or destroyMethod
as a flag in @Bean
), then those methods will never be called in prototype scoped beans.附带说明一下,如果您有任何挂钩到 bean 生命周期的方法(例如
@PreDestroy
,实现DisposableBean
接口,或作为destroyMethod
中的标志的@Bean
),那么这些方法将永远不会在原型范围的 bean 中调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.