Announcement Announcement Module
No announcement yet.
initialization & destroy methods with non-singleton beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • initialization & destroy methods with non-singleton beans


    Is it possible to correctly initialize and destroy non-singleton beans with Spring?

    I have this context:

    	<bean id="commandBean" class="test.CommandBean" singleton="false" init-method="init" destroy-method="destroy"/>	
    	<bean id="appBean" class="test.AppBean">
    		<lookup-method name="createCommandBean" bean="commandBean"/>
    AppBean is:

    public class AppBean {
    	protected CommandBean createCommandBean() { 
    		return null; 
    	public void execute() {
    CommandBean is:

    public class CommandBean {	
    	private Log log = LogFactory.getLog(getClass());	
    	public void init() {"CommandBean init : "+this);
    	public void destroy() {"CommandBean destroy : "+this);
    I use it with this startup code:

    public class TestApp {
    	public static void main(String[] args) throws Exception {
    		AbstractApplicationContext ctx = 
    			new ClassPathXmlApplicationContext(new String []{"test/context.xml"});
    On running I see:

    INFO  CollectionFactory                - JDK 1.4+ collections available
    INFO  XmlBeanDefinitionReader          - Loading XML bean definitions from class path resource [test/context.xml]
    INFO  ClassPathXmlApplicationContext   - Bean factory for application context [;hashCode=29315749]: defining beans [commandBean,appBean]; root of BeanFactory hierarchy
    INFO  ClassPathXmlApplicationContext   - 2 beans defined in application context [;hashCode=29315749]
    INFO  ClassPathXmlApplicationContext   - Unable to locate MessageSource with name 'messageSource': using default [[email protected]52]
    INFO  ClassPathXmlApplicationContext   - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.[email protected]18a7efd]
    INFO  DefaultListableBeanFactory       - Pre-instantiating singletons in factory [ defining beans [commandBean,appBean]; root of BeanFactory hierarchy]
    INFO  CommandBean                      - CommandBean init : [email protected]
    INFO  CommandBean                      - CommandBean init : test[email protected]
    INFO  ClassPathXmlApplicationContext   - Closing application context [;hashCode=29315749]
    INFO  DefaultListableBeanFactory       - Destroying singletons in { defining beans [commandBean,appBean]; root of BeanFactory hierarchy}
    So, there are no CommandBean.destroy calls. Is is possible to execute some code in non-singleton beans on closing context in another way?

  • #2
    This might help.

    There is one quite important thing to be aware of when deploying a bean in the prototype scope, in that the lifecycle of the bean changes slightly. Spring cannot (and hence does not) manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. This means that while initialization lifecycle callback methods will be (and are) called on all objects regardless of scope, in the case of prototypes, any configured destruction lifecycle callbacks will not be called. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean(s) are holding onto. (One possible way to get the Spring container to release resources used by singleton-scoped beans is through the use of a bean post processor which would hold a reference to the beans that need to be cleaned up.)


    • #3
      In addition to Karls comment

      With just singleton="false" it isn't possible. After creation the bean leaves the Spring managed container and is on it's own.

      You might want to take a look at scoped proxies. Some (request at least) of them have complete lifecycle management by Spring.


      • #4
        If you intrested in the details, this contains a blog link that might be of use.