03 December 2019

Spring contexts and Servlets


applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp. This means is transversal.

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

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


The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).

Therefore resources that need to be injected transversal can be set in applicationContext.xml, ie:

  <import resource="classpath*:spring/jwt-security-context.xml"/>
  

from: https://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring-frame



Also Servelts using Spring DI:

- https://stackoverflow.com/questions/35255052/spring-service-not-injected-in-web-servlet
- http://www.javavillage.in/spring-ioc-on-servlets.php

No comments :

My Blog List

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.