
1. Overview
In this article, we will learn the usage of the registerSingleton method defined by the ConfigurableBeanFactory
class. The ConfigurableBeanFactory
is a configuration interface to be implemented by most bean factories.
You must not directly use the ConfigurableBeanFactory
in your normal application code instead, use the sub-interfaces or implementations. Here, we will use the most common implementation DefaultListableBeanFactory
to explain the registerSingleton
method.
2. Bean Definitions
A BeanFactory
configuration comprises, at its most basic level, definitions of one or more beans that the BeanFactory
must manage.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="sampleBean" class="com.tedblob.beanfactory.models.SampleBean"> <constructor-arg value="1000" /> </bean> </beans>
3. Spring DefaultListableBeanFactory Instantiation
You can instantiate the DefaultListableBeanFactory
explicitly by referring to the previously written bean definition:
The FileSystemResource
return the absolute resource path and look for the resource in the file system such as C:/.../Core/IOC/beanfactory/src/main/resources/package/config. xml
.
public class BeanfactoryApplication { public static void main(String[] args) { DefaultListableBeanFactory beanDefinitionRegistry = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry); reader.loadBeanDefinitions(new FileSystemResource("//Core//IOC//beanfactory//src//main//resources//config.xml")); SampleBean sampleBean = beanDefinitionRegistry.getBean(SampleBean.class); System.out.println(sampleBean.getValue() + ""); } }
The ClassPathResource
looks for the resource on the classpath files inside /WEB-INF/classes.
public class BeanfactoryApplication { public static void main(String[] args) { DefaultListableBeanFactory beanDefinitionRegistry = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry); reader.loadBeanDefinitions(new ClassPathResource("config.xml")); SampleBean sampleBean = beanDefinitionRegistry.getBean(SampleBean.class); System.out.println(sampleBean.getValue() + ""); } }
Note that we have used the getBean
method of the BeanFactory
implementation to access the beans. The BeanFactory
uses lazy initialization whereas ApplicationContext
uses eager initialization. You can refer to this article to see the differences between ApplicationContext vs BeanFactory.
The BeanFactory
creates the beans when you invoke or use the beans such as getBeans()
method. You can refer to this article to understand more about BeanFactory lazy loading.
3.1. registerSingleton method
The bean definitions provide information on how to create a specific bean for the BeanFactory
. However, you can also allow registering existing bean objects that are created outside the factory (by custom code).
DefaultListableBeanFactory
supports this through the registerSingleton
method, as defined by the org.springframework.beans.factory.config.ConfigurableBeanFactory
interface.
For example, the following code registers the CustomBean
object using the registerSingleton
method.
public class BeanfactoryApplication { public static void main(String[] args) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource("config.xml")); beanFactory.registerSingleton("customBean", new CustomBean("custom bean")); CustomBean customBean = beanFactory.getBean(CustomBean.class); System.out.println(customBean.getValue() + ""); } }
Note that this customBean
singleton is not available in BeanDefinition
as we haven’t registered this through bean definition. You can verify this by calling the getBeanDefinitionNames
method on BeanFactory
.
System.out.println(Arrays.asList(beanFactory.getBeanDefinitionNames())); // prints only [sampleBean] // customBean
3.2. Destroy singleton
You can destroy the singleton bean by calling the destroySingleton
method on the BeanFactory
.
//Destroy a singleton bean from context beanFactory.destroySingleton(customBean);
4. Conclusion
To sum up, we have learned the Spring registerSingleton method. You can find code samples of this article in our GitHub repository.