8

I am studying for the Spring Core certification and I have following doubt about this question:

What is meant by “container” and how do you create one?

I know that the Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete lifecycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application. These objects are called Spring Beans which we will discuss in next chapter.

And I know that there exist 2 containers:

  • Spring BeanFactory Container: This is the simplest container providing basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.

  • Spring ApplicationContext Container: This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.

Ok...this is pretty clear for me but what is the correct answer about How to create a container?

I think that it is automatically created by the Spring when it reads the configuration class or the XML configuration file.

Or not? What am I missing?

Ravi K M
  • 77
  • 4
  • 16
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

5

In short, "The Container" is a Spring instance in charge of managing the lifecycle of your beans.

To create one, basically, you should do something like

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");

Remember replacing /application-context.xml by the file where you define your own Spring beans.

Take a look at http://www.springbyexample.org/examples/intro-to-ioc-creating-a-spring-application.html

You could also substitute the xml by a configuration class. On that case you should have something like this:

@Configuration
public class Myconfig{

   @Bean 
   public MyBean myBean(){
      return new MyBean();
   }
}

For this, take a look at http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
Andres
  • 10,561
  • 4
  • 45
  • 63
  • ok...this is if I use an XML configuration, but if it is used a Java configuration class? or by annotations? – AndreaNobili Mar 05 '15 at 16:38
  • 4
    @AndreaNobili there is always something that will end up instantiating a `XXXApplicationContext` (annotation ou Java config based). It can be you (as @Andres does), it can be the Servlet Container (with a webpp listener or a Spring based Servlet), it can be a framework (Apache CXF does this), but in the end something will create its appropriate instance of `ApplicationContext` – GPI Mar 05 '15 at 16:41
  • @Andres What about a Spring MVC application where you do not use ApplicationContext but you use dispatcher servlet. When does container get created in that case? – Arun Raaj Nov 12 '17 at 07:57
  • @ArunRaaj Then you have at least two contexts. Take a look at this https://stackoverflow.com/questions/18682486/why-does-spring-mvc-need-at-least-two-contexts – Andres Nov 12 '17 at 22:10