Home » xmlbeanfactory deprecated

xmlbeanfactory deprecated

  • by
xmlbeanfactory deprecated

1. Overview

In this article, we will learn the alternative to the deprecated XmlBeanFactory.

2. BeanFactory

The Spring framework comes with two IOC containers:

  1. BeanFactory
  2. ApplicationContext

The BeanFactory is the root interface for accessing a Spring bean container whereas ApplicationContext extends the BeanFactory and its functionalities.

The Spring IOC container gets its instructions on what bean objects to instantiate, configure, and assemble by reading configuration metadata such as XML, annotation-based or Java-based configurations.

It lets you express the objects that compose your application and the rich interdependencies between those objects. See this bean instantiation article to understand more.

3. XmlBeanFactory

The most commonly used BeanFactory implementation is the XmlBeanFactory class. This container reads the configuration metadata from an XML file and uses it to create a fully configured Spring application.

4. XmlBeanFactory deprecated

Now, XmlBeanFactory class is deprecated as of Spring 3.1 in favor of DefaultListableBeanFactory and XmlBeanDefinitionReader. You can go for any of the following solutions:

  1. DefaultListableBeanFactory and XmlBeanDefinitionReader
  2. ClassPathXmlApplicationContext

The ApplicationContext is a sub-interface of BeanFactory and so you can use the ClassPathXmlApplicationContext class instead.

This ClassPathXmlApplicationContext loads the bean definitions from the provided XML file and automatically refreshes the context.

Before checking out these solutions, first, let’s create a sample class and its instance needs to be managed by the Spring IOC container.

We will instruct the BeanFactory to instantiate using the constructor.

public class SampleBean {
    private int value;
    public SampleBean(int value) {
        System.out.println("SampleBean initialized");
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
}

Now, let’s create an XML configuration by defining the beans that need to be managed by container.

<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="employee" class="com.tedblob.beanfactory.models.SampleBean">
        <constructor-arg value="1000" />
    </bean>
</beans>

4.1. DefaultListableBeanFactory and XmlBeanDefinitionReader

You can use DefaultListableBeanFactory and XmlBeanDefinitionReader for reading the bean definitions from the XML instead of the deprecated XmlBeanFactory class.

BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("config.xml"));

4.2. ClassPathXmlApplicationContext example

In the below test case, we are loading the ApplicationContext pointing to the config.xml configuration

@Test
public void testApplicationContextLazyInitialization() {
	BeanFactory beanFactory = new ClassPathXmlApplicationContext("config.xml");
	SampleBean sampleBean = beanFactory.getBean(SampleBean.class);
}

If you execute the above application, it displays the following logs in the console.

Note that this ClassPathXmlApplicationContext is an implementation of the ApplicationContext so beans are loaded eagerly. To know the differences between ApplicationContext and BeanFactory, refer to this article.

If you remove the getBeans method invocation and execute, you would still see the SampleBean being initialized eagerly.

22:33:33.157 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c818063
22:33:33.550 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [config.xml]
22:33:33.624 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'employee'
SampleBean initialized
1000

5. Conclusion

To sum up, we have learned to use an alternative solution to the deprecated XmlBeanFactory implementation of BeanFactory interface. You can find code samples of this article in our GitHub repository.