Spring
May 27, 2020

M01 Q06 How are you going to create a new instance of an ApplicationContext?

There are different ways to create application context in Spring and they are divided into three main groups.

Non-Web Applications:

  • AnnotationConfigApplicationContext
  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext

Web Applications:

  • Servlet 2 – web.xml, ContextLoaderListener, DispatcherServlet
  • Servlet 3 – XmlWebApplicationContext
  • Servlet 3 - AnnotationConfigWebApplicationContext

Spring Boot:

  • SpringBootConsoleApplication – CommandLineRunner
  • SpringBootWebApplication – Embedded Tomcat

Non-web application context

AnnotationConfigApplicationContext - example A

The first way to create the application context is through the AnnotationConfigApplicationContext is when we wanted to use the annotations to annotate our Spring beans to do the injection to the constructor or into the setters.

In the example above, we are using ConfigurationComponentScan.class with annotation @ComponentScan to discover all beans.

We don't need to supply the name of the package because ConfigurationComponentScan is in the same package with beans.

AnnotationConfigApplicationContext - example B

In example B, instead of using @ComponentScan, I have a @Configuration and this configuration supplies the beans.

This way you need to list all of the classes you need to create the instances and then Spring will manage these objects. Usually when we are doing this is when we have some third party library that is not Spring enabled but we still want Spring to manage the classes from this third party library.

AnnotationConfigApplicationContext can supply:

  • Configuration class to the constructor
  • path to package with beans to the constructor
  • using scan() and refresh() methods, like this example

After the scan() method, you can also register() or registerBean() methods to manually setup the context.

ClassPathXmlApplicationContext

The second way is to use XML-file to describe all of our beans. And this option is when XML-file exists on the classpath.

The XML-file has a list all of the beans and properties to inject into our beans.

FileSystemXmlApplicationContext

And the third way is when we are using XML-file but this file exists within the file system, not necessarily on our classpath.

The main difference is that instead of having to provide a file from classpath we can provide the file that is located anywhere on the system.

Web application context

Servlet 2 API

This is the case when you are using the containers or application servers, for example like Tomcat. And basically we have web.xml file and we are using ContextLoaderListener and DispatcherServlet and all of our beans are described within the XML-file.

In the XML-file the servlet is registered. This servlet will be handled by the DispatcherServlet from the Spring Framework.

Also, it will be mapping all of the requests into the servlet. And then servlet will user ContextLoaderListener that will load the application context from beans.xml

This beans.xml is the same as in the previous example. But there is one additional bean SpringController. It will say hello on the web.

Servlet 3 API with XML

The second way is to use servlet 3 API and instead of using web.xml, we are using the ApplicationInitializer. And we use XmlWebApplicationContext to load XML-file from our classpath.

As you can see we still have web.xml but it is empty.

All of the initialization is happening in XmlWebApplicationInitializer in which we need to implement WebApplicationInitializer. And here we can configure all of the servlets that we want to register. And also we initialize XmlWebApplicationContext.

And in this example, we are using beans.xml to describe all of our beans. It is the same as in the previous one. But the difference is that I am not doing initialization from web.xml, I am doing initialization from WebApplicationInitializer.

Servlet 3 API with annotations

And the third way is to use servlet 3 API with ApplicationInitializer and AnnotationConfigWebApplicationContext.

So here we have empty web.xml as well and don't have beans.xml. Because all of the initialization is happening on the WebApplicationInitializer.

However, instead of creating XMLWebApplicationContext, I am using AnnotationConfigWebApplicationContext. Then I am registering the ConfigurationComponentScan which contains @ComponentScan which will discover all of my beans including Controller.

Spring Boot context

Spring Boot allows us to create console applications with the command line runner. And it allows us to create web applications with the embedded Tomcat

Spring Boot CLI

This way is very similar to the AnnotationConfigWebApplicationContext because we create a class that implements the CommandLineRunner interface.

It has annotation @SpringBootApplication, which basically has @ComponentScan

Spring Boot Web

It is also similar to AnnotationConfigWebApplicationContext and we create SpringBootWebApplication class and all of the beans will be discovered.

The difference is that we are not implementing CommandLineRunner and also we have SpringController added.