Reputation: 57
What IOC container is used in Spring MVC and can we use more than 1 container and how ?
I know that we can use multiple IOC container using spring. But can we use same in Spring MVC.
Upvotes: 1
Views: 1728
Reputation: 2626
For what purpose Container is Used:
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. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code. The following diagram is a high-level view of how Spring works. The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application.Check for more information here
Can we use more than 1 container & can we use them is Spring MVC:
Answer is YES
Typicaly,in Spring MVC project there are two "containers": One created by ContextLoaderListener and the other created by DispatchServlet.
Have a look at Spring Documentation:
The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.
Again from official Doc:
In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.
Did You noticed that the DispatcherServlet's WebApplicationContext inherits all the beans already defined in the root WebApplicationContext?
It may give you some idea how these containers are used with each other.
Upvotes: 2