Loading Context in Spring Using Web.Xml

Loading context in Spring using web.xml

From the spring docs

Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web.xml and use a contextConfigLocation to set which context files to load.

The <context-param>:

<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>

You can then use the WebApplicationContext to get a handle on your beans.

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");

See http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html for more info

How to correctly load context in Spring using web.xml and Tomcat

The problem was in the configuration files. I tried moving the META-IN directory around but without any success, then i changed this line:

<import resource="classpath*:/test-prova-spring-configuration.xml" /> 

to:

<import resource="classpath*:/META-INF/test-prova-spring-configuration.xml" /> 

And suddenly the Server Tomcat gave me some output around some missing class for the configuration (they were included in my maven pom.xml but were tagged as test, as i was using the configuration just for test scope previously). When I toke off the test scope from the pom.xml every class was working fine. I just had to update the maven project and clean the tomcat project to get it to work. So it was a problem of the "classpath" not being recognized. The strange thing is that Tomcat didn't gave any output about the xml not being found. There should be a way to log the configuration loading process to fix that. I took some inspiration from the answer of this question Printing path of spring.xml in classpath .

Loading context in web.xml

<context-param>
  • Is written outside <Servlet> tag and is inside <webapp> tag.
  • The values decalred will be available to the whole application
  • Any servlet in the application (declared in the web.xml) can access the values
  • So we use this when we want to share the the same set of values across the servlet in the application such as Data base configuration details.
  • You can use public String getInitParameter(String name) method of ServletContext interface to get value.
  • getServletContext() method of ServletConfig interface returns the object of ServletContext.
  • getServletContext() method of GenericServlet class returns the object of ServletContext.
  • Example 1 : ServletContext application=getServletConfig().getServletContext();
  • Example 2 : ServletContext application=getServletContext();

<init-param> .

  • Is written inside <Servlet> tag.
  • The values declared will be available only to the servlet.
  • You can use public String getInitParameter(String name) method of ServletConfig interface to get value.
  • getServletConfig() method of Servlet interface returns the object of ServletConfig.
  • Example : ServletConfig config=getServletConfig();

Order of loading contextConfigLocation in web.xml of Spring Servlet project

generalApplicationContext.xml is the one that will be loaded first because it is the ApplicationContext loaded with the ContextLoaderListener

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

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

specificApplicationContext.xml is actually a Child Context of the Above loaded generalApplicationContext.xml and it will be a WebApplicationContext

<servlet>
<servlet-name>my-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>my-servlet</servlet-name>
<url-pattern>/foo/*</url-pattern>
</servlet-mapping>

And yes the order of loading does matter. Because when the parent context is loaded all the required dependencies must be fulfilled.

applicationContext.xml is not getting loaded when I have kept the spring-servlet.xml in Web application

The applicationContext.xml file is not loaded by default unless you configure spring ContextLoaderListener in the web.xml or other similar configuration file.

I am assuming that the applicationContext.xml is present inside WEB-INF folder.

  <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>

If you have multiple configuration files, you can specify them as , separated values like

<param-value>
classpath:applicationContext.xml,
classpath:securityContext.xml
</param-value>

jboss spring applicationContext load on start in web.xml

For spring MVC you can load configuration xml form web.xml like below
it will load while application startup

<!-- Spring MVC     -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

How to add application context in web.xml

You're missing spring-web.jar in your classpath.

Try to add this in your maven configuration:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<type>jar</type>
<scope>compile</scope>
</dependency>

SpringMVC config applicationContext.xml should import some-servlet.xml

please change your spring bean config file user-servlet.xml name as SpringMVC-servlet.xml and remove init-param entries. And move it in WEB-INF or map resources folder as root folder in deployment assembly(for eclipse ide). It should work fine.

for UserService issue- check annotation on UserService class. It should be annotated with @Service or any other sterotype annotation and class must reside in componentScan declared package. Also User service must have a no arg default constructor or else you need to instantiate the dependent constructor arg of UserService class.



Related Topics



Leave a reply



Submit