The Spring IoC Container is used for holding the beans. It provides the infrastructure needed to manage and maintain the beans (The bean is an object instantiated and managed by the Spring container). However, what is Spring container for the developers like you and me? Quite simple, for us the Spring container is part of spring framework which can read the bean configuration files so as to take the necessary actions for creating and managing the beans for us.
Each module in the enterprise application is usually consist of lots of classes. These classes can have state (variables) and behavior (methods). As a Java developer, we have to decide – how and when to instantiate these classes in order to use their functionalities. Now, instead of us manage the objects, we can configure the Spring container to manage these objects for us.
Types of Spring IoC Containers
Spring framework provides two types of containers:
- BeanFactory
- ApplicationContext
BeanFactory
The interface BeanFactory
is the root interface which facilitates accessing the Spring IoC container. The implementation of the BeanFactory
interface facilitates holding of the bean object, which are uniquely identified by their names. The container reads the bean definition and returns the instance lazily which can further be used by the developers. The lazy instantiation denotes that the beans got instantiated on receiving the very first request.
This process of configuring the beans centrally takes away the headache of loading and reading the files by individual objects. As the application may contain number of components, the factory has no restriction on how many beans to be configured.
DefaultListableBeanFactory
is one of the very common and simple implementations of the BeanFactory
interface. It is used for registering all bean definitions first (possibly read from a bean definition file), before accessing beans. Bean definition lookup is therefore an inexpensive operation in a local bean definition table, operating on pre-built bean definition metadata objects.
ApplicationContext
BeanFactory
basically does the task of instantiation and wiring of the beans. However, today we developers need loading of the bean definitions from more than one configuration files, we also use lots of annotation based configurations and much more while developing the enterprise specific features. Unfortunately, the BeanFactory
fails to provide these advance features.
ApplicationContext
is an extension of BeanFactory
interface. It does all the work which BeanFactory
does. Additionally, it provides added useful capabilities which BeanFactory
does not have.
ApplicationContext vs BeanFactory
Following are the features provided by ApplicationContext
to choose it over BeanFactory
.
- Both provide the support for bean instantiation as well as wiring.
ApplicationContext
initializes the beans eagerly.- It automatically registers
BeanPostProcessor
andBeanFactoryPostProcessor
. - It provides a convenient way for accessing
MessageSource
. - It supports publication of events such as
ContextRefreshedEvent
,ContextClosedEvent
,ContextStoppedEvent
throughApplicationEvent
class.
ApplicationContext Types
Following are the implementations of the ApplicationContext
interface which we can be used as per the application requirements –
- ClassPathXmlApplicationContext –
It is generally used when we are dealing with standalone applications. It loads the definition from the XML files from the classpath. We can place the bean definitions in more than one XML files which then can be loaded and used for instantiating the beans by the container. - FileSystemXmlApplicationContext –
It is also used when we are dealing with standalone applications. It loads the definition from the XML files from the filesystem where the path is relative the location of the current working directory. - WebApplicationContext –
As the previous two classes provides a support for loading the bean definitions for the standalone applications, the interface WebApplicationContext provides the configuration for the web applications.Each single
DispatcherServlet
is associated with single code>WebApplicationContext. We can writewebapp-servlet.xml
file which will be specific to theDispatcherServlet
and the container will load this file fromWEB-INF
folder. - AnnotationConfigApplicationContext –
It is also used for standalone application with annotation based configuration. The class accepts input as classes which have applied annotations such as@Configuration
,@Component
etc.
ApplicationContext
unless you have a really good reason for not doing so, As the ApplicationContext
includes all functionality of the BeanFactory
and lots of others as well e.g. AOP, events etc.How to Create Container using AnnotationConfigApplicationContext
Today most developers use annotation based configuration so let’s have a look how to instantiate the AnnotationConfigApplicationContext
which is very specific to annotation based configurations.
The instantiation of the AnnotationConfigApplicationContext
is very simple as well as straightforward. The framework provides following four constructors for it’s instantiation.
AnnotationConfigApplicationContext()
:
The constructor create new context which will be populated throughregister()
method.AnnotationConfigApplicationContext(java.lang.Class...name_of_ annotatedClasses)
:
The constructor creates new context from the given annotated classes and it automatically refreshes the context.AnnotationConfigApplicationContext(DefaultListableBeanFactory defaultListableBeanFactory)
:
The constructor creates new context with the provided DefaultListableBeanFactory.AnnotationConfigApplicationContext(java.lang.String... name_of_basePackages)
:
The constructor creates new context by scanning the packages for bean definitions and then automatically refreshes the context.
Now, let’s write the class MyConfiguration
and annotate it by @Configuration
as shown below.
@Configuration public class MyConfiguration { public MyConfiguration() { System.out.println("The configuration is loaded successfully"); } }
Here, we have used the constructor to understand the process of instantiation of the container. The TestMyConfiguration.java
will load the configuration class to initialize the container as shown by the code snippet below.
public class TestMyConfiguration { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( MyConfiguration.class ); context.close(); } }
Output:
The configuration is loaded successfully
Drop me your questions in comments section.
Happy Learning !!
Ask Questions & Share Feedback