Hi, all.
I have a question about forcing re-creating singletons by the IoC container vs custom scopes, but I think I'll have to expose the whole scenario before asking. I'll try to make it as simple as possible.
My ServiceClass is managed by the IoC container (created by Spring IoC), and its constructor receives as parameters values from a database, retrieved by IoC container using Apache Commons Configuration:
ServiceClass will then create StubClass in its construction, and set the stub's endpoint to the value retrieved by dataSourceEndpoint. ServiceClassImpl is an autowired attribute of a general application's business class, and this business class is autowired to the web apps controller:Code:<bean id="serviceClass" class="package.ServiceClassImpl"> <!-- dataSourceEndpoint is a org.apache.commons.configuration.DatabaseConfiguration that will retrieve the endpoint from database --> <constructor-arg ref="dataSourceEndpoint" /> </bean>
Controller (has an autowired attribute) ->
___Autowired Business Class (has an autowired attribute) ->
______ServiceClassImpl (has an attribute created on its constructor) ->
_________StubClass
The problem is, as using default bean scope (singleton) Spring will create a single instance of ServiceClass, and this will never be created again, if I need to change the endpoint I'll have to update the database and then restart the entire application so this can read the new value, while the desired behaviour was having this value read again after some time (eg.: a couple of hours).
I don't know whether I misunderstood the scope's concept, but it looks like I can create a new scope for my beans, and handle on the scope implementation the timeouts. On my custom scope implementation I could check whether a bean was created a before my timeout period, and create a new one when it has expired.
And all of my beans (from controller to ServiceClassImpl) should use this new scope, so I can guarantee the ServiceClassImpl will be re-created (if I don't change all beans' scope I could have an old ServiceClassImpl attached to the business class).
Am I right? Is that the way to go?


Reply With Quote
