1

I have used Pre-loading in spring framework,so my beans are created when loaded in container. My beans instances are singleton. But I want to use lazy loading. To my understanding, when using lazy loading beans' instances are created when the bean is requested, but is not present in the container.

But my requirement is when the operation on that bean finishes, I want to garbage collect that bean instance. And again, when that bean is requested I want to create the bean again in lazy loading way.

So, as far as I know when beans are deployed in container again to create beans instance after bean instance is garbage collected, we have to redeploy it in the container. I want to garbage collect bean instance at runtime after it is used, and create its instance again when it is requested. Is it possible using spring and tomcat without redeploying?

1
  • I am not sure that lazy loading is usable at all - for example I know a near realtime trading system of a famous bank where all the beans are configured on the start(and container starts several minutes). Also I'm almost sure tha the beans you lazily load are singletons, it's unlikely they eat up more than several mibs of the memory, turning on lazy load is fhe last thing to do. If you have classloader memory leaks in tomcat then search how to fix them. If you run out of heap space then use special tools described in Java Performance book. Commented Jan 30, 2013 at 12:52

2 Answers 2

2

You need to use prototype scoped beans - these are beans that are created when requested, and are garbage collected like regular objects.

Take a look at the documentation page. You can declare a bean as prototype be setting the scope to prototype:

<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
Sign up to request clarification or add additional context in comments.

2 Comments

My one confusion. suppose if for accountService i have declared destroy-method="cleanResource"> in scope prototype .So,when the requested operation on this bean finishes ,then it goes for garbage collection.If i declare destroy-method="cleanResource"> then does it call cleanResource method on that bean and it is garbaged collected Or it is directly garbaged collected without calling cleanResource method .Suppose that resource is some Input/output and i want to release that resource before it is garbaged.OR we should close the application context to call this method cleanResource on this bean.
@abishkar Spring only creates prototype beans, but does not call the destroy method. If using such a bean, you are responsible for manually cleaning after your bean. Closing the application context won't help, you have to call whatever destroy methods you have on that object.
2

Bean scoping is probably the most straightforward way to do it. You'll probably want "prototype", but "session" and "request" are also available inside of a web container.

If you're using "session" or "request" and are injecting the bean into a singleton bean, you'll need to use <aop:scoped-proxy/>:

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
    <aop:scoped-proxy/>
</bean>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.