DEV Community

Tristan Elliott
Tristan Elliott

Posted on • Updated on

What the heck is the Spring IoC container?!?!

Introduction

  • This series will be everything I learned from reading the Spring documentation. If you would also like to spend hours of your life staring at technical documents, you can find the docs HERE

Inversion of control and dependency management.

  • So loosely speaking our spring application is nothing more then a Java application. As you and I both know Java applications typically consist of objects that collaborate to form the application and objects in application all have dependencies. We as the developer have design patterns to handle object creation and dependency management. However, as our application grows this becomes hard to manually manage. Thankfully the Spring framework provides us with the Inversion of Control container (IOC container).

Spring IOC container

  • IOC, also know as dependency injection, is a process where objects define their dependencies, which are other objects they work with. The IOC container then injects those dependencies when it creates the bean. A bean is just an object that is managed by the IOC container. So basically the spring IOC container is responsible for instantiating, configuring, and assembling beans. How does the container know what to instantiate and configure? Very good question, and that is done with configuration metadata. We can define this metadata in 3 ways:

1) XML
2) Java annotations
3) Java code

  • For this blog post I will only be explaining Java annotations, Java code.

Annotation based container configuration.

  • Instead of using the traditional XML we can use annotations to configure the metadata. We configure the metadata by annotating the appropriate class, method or field declaration. The most common annotation being @Autowired, so when you see @Autowired` know that we are configuring the metadata for IOC container.

Java based container configuration

  • These are basically just the new annotations and they are @Bean and @Configuration . The @Bean annotation is used to indicate that this field is supposed to be managed by the IOC container. Annotating a class with @Configuration is used to indicate that its main purpose is to register beans.

Ok but what is the IOC container?

  • So, we know at a high level that it is used to managed all the beans and their dependencies. But what actually is it? what class? When is it instantiated? What methods does it have? These kind of questions mean its time to get down and dirty with the documentation. Thankfully spring has some of the best documentation I have ever seen....update your documentation React!!!!!!!.

  • So lets just start at our main class and read the documentation until we find something that answers our questions.

@SpringBootApplication

  • When reading the documentation we can find that this annotation is the combination of 3 other annotation:

1)@Configuration : allows us to register beans

2)@EnableAutoConfiguration : enables Spring's auto configuration mechanism. This enables Spring to automatically configure the Spring application based on the JAR dependencies. For us, I believe that is everything in the POM file.

3)@ComponentScan: Identifies which classes should be managed by Spring.

  • well, That did not give us many answers so lets move on to SpringApplication.

SpringApplication

  • When reading the documentation for this class, we get the following definition, By default this class will perform the following steps:

1) Create the appropriate ApplicationContext instance

2) Register a CommandLinePropertySource to expose command line arguments as spring properties

3) Refresh the application context, loading all singleton beans

4) Trigger any CommandLineRunner beans

  • We are now only interested in 1) and 3).

What is ApplicationContext ?

  • Reading the docs we find this beauty, ApplicationContext represents the Spring IOC container and is responsible for instantiating, configuring and assembling beans . Which is awesome, we now have a solid definition on what the IOC is. However, upon further looking into the docs you will notice that ApplicationContext is an interface and it needs to be implemented by some class before it is instantiated. Unfortunately, I could not find definitive proof on what class implements and then instantiates ApplicationContext. I suspect that this is an implementation detail that the documentation does do define. Although, I could also just not be looking in the right places.

What is a singleton bean.

  • Well, it turns out when we define a bean we can also define its scope and if we do not, the scope is defined as Singleton. So basically, all the beans that I have defined up to this point have been singletons. This means that the Spring IOC container will create only one instance of this bean and cache it for later use.

What exactly is the Spring IOC container?

  • Explained at a high level we know that it is a component of Spring whose job is to manage beans and their dependencies. However, at a lower implementation level I believe that it goes something like this, SpringApplication.run() is called which will run our application. Then at some point Spring will implement the ApplicationContext interface and instantiated it to create what we know as the IOC container. Spring will then refresh the ApplicationContext and load all the singleton beans that we have defined. Thus creating our IOC that we all know and love.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)