[英]Scope of Spring beans enforcement
I have a scenario for example. 我有一个例子。
<bean id="xyzService" class="XyzServiceImpl" scope="prototype">
<property name="aDependency" ref="aDependency" />
<property name="bDependency" ref="bDependency" />
</bean>
<bean id="useService" class="UseServiceImpl">
<property name="xyzService" ref="xyzService"/>
</bean>
Java Class : Java类:
public class XyzServiceImpl implements XyzService{
private ADependency aDependency= null;
private BDependency bDependency= null;
// getters and setters...
}
public class UseServiceImpl implements UseService {
private XyzService xyzService= null;
// getters and setters...
xyzService.doSomething();
}
Now every time inside the UseServiceImpl I expect a new Instance of xyzService, but i always return the same singleton instance. 现在,每次在UseServiceImpl中,我都希望有一个xyzService的新实例,但是我总是返回相同的单例实例。 Also there is a scenario that the
aDependency
and bDependency
may internally have again some more references to other beans. 还有一种情况,
aDependency
和bDependency
可能在内部再次具有对其他bean的更多引用。
Now I have a question like how do I get an new Instance of xyzService. 现在,我有一个问题,例如如何获取xyzService的新实例。 Am I doing something wrong?
难道我做错了什么?
By default scope of spring bean is singleton
, You need to mark the scope prototype
to instruct spring 默认情况下,spring bean的作用域是
singleton
,您需要标记作用域prototype
以指示spring
<bean id="beanId" class="some.class.Name" scope="prototype"/>
Spring will create new instance on each request of Bean Spring将在Bean的每个请求上创建新实例
See 看到
I could easily find the solution by implementing the ApplicationContextAware
Interface which has the getter and setter method for context. 我可以通过实现
ApplicationContextAware
接口轻松找到解决方案,该接口具有用于上下文的getter和setter方法。 From the context I can say getBean and get the new Instance 从上下文中,我可以说getBean并获取新的实例
public class UseServiceImpl implements UseService,ApplicationContextAware {
private ApplicationContext context;
XyzService xyzService= context.getBean(XyzServiceImpl.class);
// getter and setter for applicationContext
private XyzService xyzService= null;
// getters and setters...
xyzService.doSomething();
}
If you have the following: 如果您具有以下条件:
<bean id="xyzService" class="XyzServiceImpl" scope="prototype">
<property name="aDependency" ref="aDependency" />
<property name="bDependency" ref="bDependency" />
</bean>
<bean id="useService1" class="UseServiceImpl">
<property name="xyzService" ref="xyzService"/>
</bean>
<bean id="useService2" class="UseServiceImpl">
<property name="xyzService" ref="xyzService"/>
</bean>
Then you should be able to verify that the xyzService
property for useService1
and useService2
do contain different instances of xyzService
. 然后,你应该能够验证
xyzService
属性useService1
和useService2
不包含的不同实例xyzService
。 That's the effect of declaring xyzService
to be scoped as a prototype. 这就是声明
xyzService
被限制为原型的效果。 If you really want new instances of the xyzService
bean to be available during the lifetime of the useService
bean, I think you'll need a different approach - take a look at the documentation for Method injection . 如果你真的想要的新实例
xyzService
bean中的生命周期中可用useService
豆,我认为你需要一个不同的方法-看看对于文档方式注入 。
In your example, every time you request spring container an instance of userService , it will return the singleton instance and injecting a new instance of xyzService . 在您的示例中,每次您请求spring容器一个userService实例时,它将返回单例实例并注入xyzService的新实例。
However, when spring creates a new instance of xyzService , it will use the singleton instance of aDependency and bDependency unless otherwise they are also defined as prototype . 但是,当spring创建xyzService的新实例时,它将使用aDependency和bDependency的单例实例,除非另外将它们也定义为prototype 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.