spring初始化bean有多长时间了?(How long does an spring initialized bean live?)

编程入门 行业动态 更新时间:2024-10-24 07:26:21
spring初始化bean有多长时间了?(How long does an spring initialized bean live?)

当我在服务器上运行bean的方法的第一个小问题(比方法A)时,一切似乎都没问题,但是当第二次运行这个carrierRESTWS bean的任何请求时(假设方法B),使用的dao是相同的carrierDAO实例。 我怎样才能避免出现这个问题并且每次调用这个carrierRESTWS bean时注入使用dao的新实例?

xml文件中的Bean配置:

<bean id="carrierRESTWS" class="ar.com.anovo.controllers.rest.CarrierRESTWS"> <property name="carrierDAO" ref="carrierDAO"/> </bean> <bean id="carrierDAO" class="ar.com.anovo.dao.CarrierDAO"></bean>

When i run the first petittion of a bean's method (let's say method A) on the server everything seems ok, but when running for the second time any petition on this carrierRESTWS bean (let's say method B), the dao being used is the same carrierDAO instance. How can i avoid having this problem and making the injection use a new instance of the dao each time this carrierRESTWS bean is being called?

Beans configuration inside xml file:

<bean id="carrierRESTWS" class="ar.com.anovo.controllers.rest.CarrierRESTWS"> <property name="carrierDAO" ref="carrierDAO"/> </bean> <bean id="carrierDAO" class="ar.com.anovo.dao.CarrierDAO"></bean>

最满意答案

将“carrierDAO”的范围设置为“prototype”:

<bean id="carrierDAO" class="ar.com.anovo.dao.CarrierDAO" scope="prototype" />

一旦需要注射,这将创建一个新实例。

有关范围的更多信息,请参阅Spring Doc 。

Set the scope of "carrierDAO" to "prototype":

<bean id="carrierDAO" class="ar.com.anovo.dao.CarrierDAO" scope="prototype" />

This will create a new instance, once an injection is required.

More about scopes can be found in the Spring Doc.

更多推荐

carrierRESTWS,dao,carrierDAO,bean,电脑培训,计算机培训,IT培训"/> <meta name=&

本文发布于:2023-04-27 14:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长时间   有多   初始化   bean   spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!