How Spring Ioc Container Interacts with Tomcat Container

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

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

I could not understand what the interviewer had in mind, and remained silent. Can someone clarify what this question is, and what a reasonable answer there may be?

+4
source share
3 answers

A spring web application will define the spring dispatch servlet in its configuration, the apc tomcat container initializes this servlet, the dispatcher servlet, in turn, initializes the application context. There is no direct interaction between the tomcat container and the IOC spring container.

+4
source

There are two main aspects of binding Spring to servlets. First you need to load the Spring application context, and secondly, you need to expose these Spring-loaded objects in the Servlet. There are many ways to do this, and in formats like CXF , there is native support for Spring.

ContextLoaderListener HttpRequestHandlerServlet .

:

web.xml:

<web-app>
...
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>servletBean</servlet-name>
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
  </servlet>
...
</web-app>

/WEB -INF/applicationContext.xml:

<beans>
..
  <!-- Note: YourServletClass must implement HttpRequestHandler -->
  <bean id="servletBean" name="servletBean" class="yournamespace.YourServletClass">
    ...
  </bean>
...
</beans>
+1

Spring DispatcherServlet . DipatcherServlet HttpServlet , , . DispatcherServlet WebApplicationContext. Spring IOC WebApplicationContext ( ). ApplicationContext, ContextLoaderListener. ApplicationContext WebApplicationContext () -. IOC ApplicationContext WebApplicationContext (s).

ServletContext remains the only way to interact for all web containers.

+1
source

All Articles