Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Spring Ioc container interacts with Tomcat container

I am familiar with the Spring Framework and have done some work in it.

In one of my interviews, I was asked "there is a web application deployed in Apache Tomcat; tell me how does the "Tomcat container" (used for servlets) interact with "Spring IoC container" (used for Spring beans)?"

I couldn't understand what the interviewer meant by that and was left speechless. Can someone please clarify what this question was about and what a reasonable answer to it might be?

like image 529
Anand Avatar asked Dec 16 '14 16:12

Anand


People also ask

How does IoC container work in Spring?

Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application.

Is Spring container and IoC container same?

An IoC container is a common characteristic of frameworks that implement IoC. In the Spring framework, the interface ApplicationContext represents the IoC container. The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.

How does Spring achieve or IoC?

Spring IoC is achieved through Dependency Injection. Dependency Injection is the method of providing the dependencies and Inversion of Control is the end result of Dependency Injection. IoC is a design principle where the control flow of the program is inverted.

How does Tomcat servlet container work?

Once the request has been mapped to the appropriate servlet, Tomcat checks to see if that servlet class has been loaded. If it has not, Tomcat compiles the servlet into Java bytecode, which is executable by the JVM, and creates an instance of the servlet. Tomcat initializes the servlet by calling its init method.


1 Answers

A spring web-app will define a Spring Dispatcher Servlet in its config, the apache tomcat container will initialise this servlet, the dispatcher servlet in turn initialises the application context. There is no direct interaction between the tomcat container and the Spring IOC container.

like image 156
emeraldjava Avatar answered Oct 06 '22 00:10

emeraldjava