[英]Singleton bean instantiated multiple times in Spring MVC?
Does anyone know why this bean is getting instantiated multiple times? 有谁知道为什么这个bean被多次实例化? I only ever want one instance of it but every time the controller runs the construcutor is called again.
我只想要它的一个实例,但是每次控制器运行构造函数时,都会再次调用它。
Here is the definition in my applicationContext.xml 这是我的applicationContext.xml中的定义
<bean id="DiameterClient" class="com.rory.diameter.client.DiameterClient" scope="singleton" init-method="start">
<constructor-arg type="java.lang.String" index="0"><value>${pcca.host}</value></constructor-arg>
<constructor-arg index="1"><value>${pcca.port}</value></constructor-arg>
<constructor-arg index="2" value="com.openwave.djgx.message"/>
<constructor-arg index="3" value="com.openwave.djgx.avp"/>
</bean>
And in my controller here is where I'm using it - I though this would only get one instance of the DiameterClient class, but it is calling its constructor each time the code below runs - any help is much appreciated: 在我的控制器中,这里是我使用它的地方-虽然我只会得到DiameterClient类的一个实例,但是每次下面的代码运行时它都会调用其构造函数-非常感谢您的帮助:
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
DiameterClient diameterClient = (DiameterClient)factory.getBean("DiameterClient");
diameterClient.send(aar);
Note, DiameterClient is not my class, and I dont want to edit it, just want to have one global instance of it per application. 注意,DiameterClient不是我的课程,我不想编辑它,只希望每个应用程序有一个全局实例。 Note also, DiameterClient extends Thread - not sure if this matters or not.
另请注意,DiameterClient扩展了Thread-不确定是否重要。
You are creating a new context each time and scope singleton
means there is one instance in context. 每次创建一个新上下文时,作用域
singleton
意味着上下文中存在一个实例。 Usually you need one context per application execution. 通常,每个应用程序执行需要一个上下文。 Move part below to a place executed once in your application:
将下面的部分移动到应用程序中一次执行的位置:
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.