3

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>
LuGaNO
  • 99
  • 1
  • 2
  • 11

3 Answers3

1

Your beans are singleton beans, so they live for as long as the Spring Container lives, which basically means for as long as your program is running, which again means for as long as your webapp is running, which could theoretically be years.

Since your controllers and your DAO classes have of course been coded to be stateless, and support multi-threading, you shouldn't have a problem with a single shared instance.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

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.

Stefan
  • 12,108
  • 5
  • 47
  • 66
0

The default scope in spring is singleton, so you need to explicitly set the scope which makes a new instance each time as @Stefan indicates with prototype.

leeor
  • 17,041
  • 6
  • 34
  • 60