7.5.3 Singleton beans with prototype-bean dependencies

When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean.

当你使用的单例域beans依赖着原型beans的时候,要意识到依赖解析是在初始化的时候进行的.因此如果你依赖注入了一个原型域bean到一个单例域bean,一个新的原型bean被初始化然后依赖注入到这个单例bean.这个原型实例时单一的实例,它永远的提供给了这个单例域bean.

However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies. If you need a new instance of a prototype bean at runtime more than once, see Section 7.4.6, “Method injection”

然而,假设你想要这个单例域bean在运行时反复的接受一个新的原型域bean的实例.你不能够依赖注入一个原型域bean到你的单例bean,因为这个注入仅发生一次,在当这个Spring容器初始化了这个单例bean,解析和注入了它的依赖的时候.如果你需要在运行时不止一次的获取到一个新的原型bean实例,请看Section 7.4.6, “Method injection”.

猜你喜欢

转载自blog.csdn.net/weixin_41648566/article/details/81126542