Can Spring "live" alongside other servlets in the same webapp? - spring

I've got the a WEB-INF/web.xml file with a couple of servlets, along with a context
listener which I use to bootstrap the application. I'd like to use Spring in this
web application. What's the best way to work Spring into this so I can use Spring's
injection mechanisms throughout the entire application - even in the servlets which
exists today?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Company's XMLRPC service</display-name>
<!-- Servlet Listeners -->
<listener>
<listener-class>com.company.download.context.DefaultServletContextListener</listener-class>
</listener>
<!-- Servlet Declarations -->
<servlet>
<servlet-name>DefaultTrackDownloadServlet</servlet-name>
<servlet-class>com.company.download.web.impl.DefaultTrackDownloadServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DefaultXmlRpcServlet</servlet-name>
<servlet-class>com.company.download.web.impl.DefaultXmlRpcServlet</servlet-class>
</servlet>
<!-- Servlet Configurations -->
<servlet-mapping>
<servlet-name>DefaultTrackDownloadServlet</servlet-name>
<url-pattern>/track</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DefaultXmlRpcServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Can Spring “live” alongside other servlets in the same webapp?
Yes, Spring MVC is basically just a DispatcherServlet that can make use of a ContextLoaderListener.
These two classes are already setup to interact with one or more ApplicationContext instances and have Spring manage the declared beans.
Your custom Servlet classes are not. If you need to inject a bean into your own Servlet instances, you need to get a reference to the ApplicationContext from the ContextLoaderListener and get the beans you want. There are a few options, whether you do it yourself or use built-in features.
The ContextLoaderListener stores the ApplicationContext it loads into a ServletContext attribute named
org.springframework.web.context.WebApplicationContext.ROOT
So you can retrieve it with that (there's a constant for easy use)
ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
Other options exist, see some of them here:
Autowiring in servlet
I want to inject an object in servlet using Spring

Related

spring mvc; contextConfigLocation load order

I'm reconfiguring a webapp. I want to move everything out of dispatcher servlet into ContextLoaderListener. (This is due to changes in security configuration beyond the scope of this question)
Question 1, if I have multiple application context xml files, does it matter what order they are loaded? For example does the xml file containing context:component-scan need to be loaded before the xml file specifying DAO and service beans?
Question 2, (or is this moot?) how would I specify the order in which *_applicationContext.xml are loaded assuming that A_applicationContext.xml should be loaded before B_applicationContext.xml which should be loaded before C_applicationContext.xml
My web.xml is as follows:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>AssessmentDelivery</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentDelivery</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/*_applicationContext.xml</param-value>
</context-param>
<!-- security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Some suggestions:
For these days consider do the configuration for Spring through Javaconfig.
To answer questions 1 and 2 is very important you understand the following:
When you run the app Spring creates an Application Context where exists all the beans created and managed by Spring. Now consider that for that Application Context it should be created from two 'sub' applications contexts, normally they are 'mentioned' in the documentation how ServletApplicationContext and RootApplicationContext
The former should scan all about the Web, such as your #Controllers and #Bean's about infrastructure such as for ViewResolver etc..
The later should scan all about the Server, such as #Service and #Repositories and #Bean's about infrastructure such as for a DataSource etc.
Is very important understand the following:
ServletApplicationContext --> RootApplicationContext
It means the former can get access the latter (it about use dependencies i.e: a #Controller needs a #Service). Therefore it reflects that the Web side can access the server side.
Once said this the following is not possible
RootApplicationContext --> ServletApplicationContext
has no sense that a Bean from the server side want access the web side (a bad practice)
Long time ago I don't use web.xml but
DispatcherServlet + contextConfigLocation (through <init-param>) represents the ServletApplicationContext
ContextLoaderListener + contextConfigLocation (through <context-param>) represents the RootApplicationContext
It does not matter if the beans are declared through:
XML
JavaConfig
annotations #Controller etc.
Spring manages the cycle about in what order the beans are created. So do not matter how the .xml files (in your case) are declared (about the order).

How to initialize SpringWebAplication without dispatcher servlet?

I'm using Spring, but not SpringMVC in a web applcaition and gotta initialize Spring Application via web.xml
I have the standard web-project structure:
WEB-INF/applicationContext.xml
WEB-INF/web.xml
web.xml is currently contains:
<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>
But my question was caused that any listener should be bound to a servlet. In my case I don't use dispatcherServlet. How to load spring WebApplicationContext, all spring beans, etc in that case? What do I write in web.xml?
A ContextLoaderListener initializes a WebApplicationContext and stores it in the ServletContext attributes under the name referenced by WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.
You can retrieve it that way.
servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
anywhere you have access to the ServletContext, after the ContextLoaderListener has finished executing.
As suggested by M.Deinum in the comments, this is such a popular use case that Spring has its own utility to do it:WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext).
You don't need to change anything in your web.xml.

Understanding spring dispatcher servlet initialization

Here is how spring documentation recomends to initialize dispatcherServlet:
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
My question is about providing an empty param-value inside the init-param tag. Despite defining that param as context-param we still provide the empty value. Therefore contextConfigLocation should be null when passes to servlet's init() method. What's wrong, correct me please.
In Spring Web Applications, there are two types of container, each of which is configured and initialized differently.
Application Context
Web Application Context
Application context is inialised by config file's that you specified in as context-params and picked up by ContextLoaderListener. This is purely i would consider as business logic related beans.
Web application context is child of application context which may or may not be present. Each DispatcherServlet will have associated WebApplicationContext and which takes spring beans from your init-params to create context.
Whatever beans are available in the ApplicationContext can be referred to from each WebApplicationContext.
Reason why we have two different bean configurations is to keep a clear separation between middle-tier services such as business logic components and data access classes (that are typically defined in the ApplicationContext) and web- related components such as controllers and view resolvers (that are defined in the WebApplicationContext per Dispatcher Servlet).

Singleton bean in two contexts spring

I have a web application context(for DISPATCHER) and also Web Services context(for MESSAGE DISPATCHER) in a web application.
I have one bean which is singleton and i need that bean in both contexts.
if i specify the bean as singleton in both contexts then it is not singleton any more.
please suggest a solution or guide me in right direction.
<servlet>
<servlet-name>ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
If I understand your question correctly, you have a web application context (e.g. *-servlet.xml) and a root application context (e.g. applicationContext.xml). Web application context extends application context, so that it can access beans from the parent, but not the other way around, so beans which need to be accessed in both should be in applicationContext.xml
See:
Difference between applicationContext.xml and spring-servlet.xml in Spring Framework and
What is the difference between ApplicationContext and WebApplicationContext in Spring MVC? and
ContextLoaderListener or not?
Edit:
In your web.xml you have two ServletContexts, but no root context. The answer to What is the difference between ApplicationContext and WebApplicationContext in Spring MVC? has an excellent explanation of this, but in short you will need to load the root application context by adding the following to web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
which will then load the root application context from applicationContext.xml. Beans in the root application context will be accessible in both ServletContexts. You would usually want only web related beans (Controllers etc) in you ServletContexts, and shared business logic in the root application context.
If the bean needs to be used in two different contexts, define it in a separate application context xml file, then create three application contexts:
Root context - contains shared bean(s)
App context 1. e.g. web app. Has the root context as a parent.
App context 2. e.g. web services interface, also has the root context as a parent.
The bean will be instantiated once when the root application context is created. Both the child app contexts can then use that singleton bean.

How to connect HttpServlet with Spring Application Context in web.xml?

I'm trying to connect my FooServlet which extends HttpServlet with the ApplicationContext which is in the same Project.
The Application Context is already used by a Wicket Servlet
It works with
servletContext = this.getServletContext();
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
(IMyBean)wac().getBean("myServiceBean")
Now I try to aviod to use explicitly Spring Classes in my Code (WebApplicationContextUtils) as it's not the IoC way.
The Wicket Servlet is connected with the Application context in the web.xml
<servlet>
<servlet-name>ExampleApplication</servlet-name>
<servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
I found the class Spring HttpServletBean but I don't know if it serves for my Case
I found a way to inject Beans in my HttpServlet (Note: I don't need a Presentation View, otherwise there are more advanced Spring Classes)
Add a ContextLoaderListener to web.xml so that Spring's root WebApplicationContext is loaded
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Configure Servlet using Springs HttpRequestHandlerServlet Class
<servlet>
<servlet-name>FooServlet</servlet-name>
<display-name>Foo Servlet</display-name>
<servlet-class>
org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
</servlet>
Let your Servlet implement the org.springframework.web.HttpRequestHandler Interface
Define your Servlet as a Bean in ApplicationContext (beanID must be same as "servlet-name").
Now it's possible to inject all necassary Beans in the Spring DependencyInjection way without dependency lookup.
I think you should use Spring utilities like
RequestContextUtils.getWebApplicationContext(request, application);
to hookup the Spring Context within your Servlet.
Agreed this is no DI/IoC, but the servlet is no bean as well !

Resources