Tuesday, May 29, 2012

Adding and Removing Spring initialized beans from Container

Yesterday one of my friend shoot a question on Spring bean management on runtime. His requirement is to remove bean for container and register it back after doing some operation.
The idea is straight, We might run into these situations depending on our business use cases complexities.

Here is a test case code snippet to remove bean and register it again to the spring container

Spring Context XML


        
	
		
			Shinu
		
	


JUnit Test class



package com.shinu.demo;

import javax.servlet.ServletContext;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class SpringWebDynamicCreationTest extends TestCase {

	final static Log logger = LogFactory
			.getLog(SpringWebDynamicCreationTest.class);

	protected static ConfigurableWebApplicationContext context;

	ServletContext mockServletContext;

	@Override
	protected void setUp() throws Exception {
		mockServletContext = new MockServletContext("/WebContent",
				new FileSystemResourceLoader());
		context = new XmlWebApplicationContext();
		context.setConfigLocations(new String[] { "/WEB-INF/config/springContext.xml" });
		context.setServletContext(mockServletContext);
		context.refresh();
		context.registerShutdownHook();
	}

	public void testAddAndRemoveObjects() throws ClassNotFoundException {
		Demo demo = getBean(context);
		assertEquals("Shinu", demo.getName());

		ConfigurableListableBeanFactory configurableListableBeanFactory = context
				.getBeanFactory();

		BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) configurableListableBeanFactory;
		// Removing the bean from container
		beanDefinitionRegistry.removeBeanDefinition("demo");
		
		// Trying to obtains bean again from container. This will throw a null		
		demo = getBean(context);
		//demo object will be null here
		assertNull(demo);
		
		// Creating and registering bean to the container		
		BeanDefinition beanDefinition = new RootBeanDefinition(Demo.class);
		beanDefinition.setAttribute("name", "Shinu");

		beanDefinitionRegistry.registerBeanDefinition("demo", beanDefinition);
		context.refresh();
	
		//Obtaining from container again
		demo = getBean(context);
		assertEquals("Shinu", demo.getName());

	}

	private static Demo getBean(ApplicationContext applicationContext) {		
		try{
			return (Demo) applicationContext.getBean("demo");
		}catch(NoSuchBeanDefinitionException e){
			return null;
		}
	}

}


1 comment: