Inversion of Control Container

Written by: Tom Spencer

Jun 26, 20223 min read
Inversion of Control

Spring IoC (Inversion of Control) Container is the core of the Spring Framework. There are two types of containers in Spring - the Bean Factory and the Application Context. ApplicationContext is an interface in the org.springframework.context package and is implemented by several classes. The ClassPathXmlApplicationContext is one such implementation.

Spring BeanFactory

Here is an overview of the classes provided by the BeanFactory:

  • FileSystemXmlApplicationContext provides beans loaded through the full path.
  • ClassPathXmlApplicationContext provides beans loaded through the CLASSPATH
  • XMLWebApplicationContext and AnnotationConfigWebApplicationContext beans are loaded through the web application context.
  • AnnotationConfigApplicationContext loads Spring beans from Annotation based configuration.
Spring BeanFactory and Dependent classes
76plE
Bean Factory vs Application Context

Spring provides two kinds of IOC container: XMLBeanFactory and ApplicationContext. I include an overview of the differences between the two below:

Bean Factory
  • Bean instantiation/wiring
  • Application Context
Application Context
  • Bean instantiation/wiring
  • Automatic BeanPostProcessor registration
  • Automatic BeanFactoryPostProcessor registration
  • Convenient MessageSource access (for i18n)
  • ApplicationEvent publication
Bean Factory and Application Context
TqcgB

Practice Implementation

image

For practice we can build a very simple application using the ClassPathXmlApplicationContext. We start with a Salutation interface with a single method message:

Screenshot 2022-06-26 at 10 09 14

We then implement two classes that implement this interface. Hello and Goodbye:

Screenshot 2022-06-26 at 10 08 55

Screenshot 2022-06-26 at 10 13 37

Next we create a simple Meeting class in order to print out our message:

Screenshot 2022-06-26 at 10 10 28

This is solid. We may want to avoid using lots of new Hello() instances in our application. The Spring ApplicationContext allows us to access a single instance of the Hello class through the ClassPathXmlApplicationContext. Let's look at how to do this.

First add spring-context to our pom.xml so that we have access to the Spring ApplicationContext from the Spring Framework.

Screenshot 2022-06-26 at 10 11 24

Next add a beans.xml file in inversion-of-control/src/main/resources. This will allow us to access the Hello class through the id hello in our Spring ApplicationContext. It helps here to think of the Spring ApplicationContext as a map of keys and values where our key is "hello" and the value is the Hello class.

Screenshot 2022-06-26 at 10 11 40

Now we have access to the ApplicationContext through the ClassPathXmlApplicationContext we can get the hello bean we have created and access the message:

Screenshot 2022-06-26 at 10 10 53

We should get the message "Hello!":

Screenshot 2022-06-26 at 10 23 53

Extra practice
  • Change the accessed class from Hello to Goodbye and print the message.
  • Code is available here inversion of control