Spring MVC 中单例 bean 多次实例化?

发布于 2025-01-03 15:07:07 字数 1060 浏览 1 评论 0原文

有谁知道为什么这个 bean 被实例化多次?我只想要它的一个实例,但每次控制器运行时,都会再次调用构造函数。

这是我的 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>

,在我的控制器中,这是我使用它的地方 - 我虽然这只会获得 DiameterClient 类的一个实例,但每次运行下面的代码时它都会调用其构造函数 - 任何帮助非常感谢:

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
DiameterClient diameterClient = (DiameterClient)factory.getBean("DiameterClient");
diameterClient.send(aar);

注意,DiameterClient 不是我的类,我不想编辑它,只想为每个应用程序拥有一个它的全局实例。另请注意,DiameterClient 扩展了 Thread - 不确定这是否重要。

Does anyone know why this bean is getting instantiated multiple times? 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

<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:

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. Note also, DiameterClient extends Thread - not sure if this matters or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2025-01-10 15:07:07

您每次都会创建一个新的上下文,并且范围 singleton 意味着上下文中有一个实例。通常,每次应用程序执行都需要一个上下文。将下面的部分移动到应用程序中执行过一次的位置:

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

You are creating a new context each time and scope singleton means there is one instance in context. 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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文