Results 1 to 4 of 4

Thread: initialization & destroy methods with non-singleton beans

  1. #1

    Default initialization & destroy methods with non-singleton beans

    Hi,

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

    I have this context:

    Code:
    <beans>	
    	<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"/>
    	</bean>	
    </beans>
    AppBean is:

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

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

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

    Code:
    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 [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=29315749]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [commandBean,appBean]; root of BeanFactory hierarchy
    INFO  ClassPathXmlApplicationContext   - 2 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=29315749]
    INFO  ClassPathXmlApplicationContext   - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@efd552]
    INFO  ClassPathXmlApplicationContext   - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@18a7efd]
    INFO  DefaultListableBeanFactory       - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [commandBean,appBean]; root of BeanFactory hierarchy]
    INFO  CommandBean                      - CommandBean init : test.CommandBean@1125127
    INFO  CommandBean                      - CommandBean init : test.CommandBean@18dfef8
    INFO  ClassPathXmlApplicationContext   - Closing application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=29315749]
    INFO  DefaultListableBeanFactory       - Destroying singletons in {org.springframework.beans.factory.support.DefaultListableBeanFactory 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    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.)
    http://www.springframework.org/docs/...opes-prototype

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    If you intrested in the details, this contains a blog link that might be of use.
    http://forum.springframework.org/showthread.php?t=32678

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •