Spring : Sharing Application Context between different servlets - spring

I have a web application and SOAP Web service in same project. I have defined two servlet xml
mvc-servlet.xml
<import resource="ws-applicationContext.xml"/>
soap-servlet.xml
<import resource="soap-applicationContext.xml"/>
Bot these applicationContext import a common file
Contents of soap-applicationContext.xml
<import resource="my-common-applicationContext.xml"/>
<context:annotation-config/>
<context:component-scan base-package="com.xyz.myproject1"/>
Contents of ws-applicationContext.xml
<import resource="my-common-applicationContext.xml"/>
<context:annotation-config/>
<context:component-scan base-package="com.xyz.myproject2"/>
my-common-applicationContext.xml has common packages which are needed by both like integration,data etc.
At start of application , since my-common-applicationContext.xml is included in both the application context, it gets loaded twice. This leads to DataConfigurations being loaded twice.
What is the best way to solve this problem also ensuring both the context have access to relevant beans ?

Figured out the right way to do it .
All common files go in common but are loaded with ContextLoaderListener. This ensures all classes are available.
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:my-common-applicationContext.xml
</param-value>
</context-param>

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

Spring project not creating spring-config.xml

I have read some tutorials about using spring, and I've seen they speak about "spring-config.xml", but when I create a project I don't have that file, I have "application-config.xml", are they the same? Is the former the updated version of the latter? I am using Eclipse as IDE
The Spring Context only defines the concept of creating a Spring configuration where you will define spring components (beans, services, etc)
The XML itself can be named whatever you want, but in the web.xml file, you have to pass the xml name you choose to the spring context listener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/thisXMLhaTheBestNameEver.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

java.lang.IllegalStateException: Root context attribute is not of type WebApplicationContext

I am deploying Portlets on Liferay 5.2.3 on Tomcat 6. I get this error only for one of the portlet.
java.lang.IllegalStateException: Root context attribute is not of type WebApplicationContext
I did some research and found out that Spring was instantiating a portlet application context when it need a web one. But in my web.xml I am only defining contextLoaderListner
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
And to top it off, if a different *.jar file was being looked up by Spring, then why would my other portlets get deployed except one?
After couple of redeployments I get that to a fix. Can someone put some light on?
The root cause seems to be a static variable in the portal/application server "hanging onto" an instance of a class from the portlet. Two common culprits are log4j and java logging, both of which are commonly used by application containters.
See log4j and the thread context classloader and http://logback.qos.ch/manual/loggingSeparation.html for more discussion of loggers. The suggestion is to use SLF4J with logback OR to be sure to put log4j.jar in your WAR file so it is in the right classloader (although some containers will thwart this solution).
Also, some other class that is present in the container may be the cause. Logging is just a common problem.
Sounds like you are not defining the contextConfigLocation? in web.xml you should also have something like this in addition to the contextLoaderListener:
<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>
Where applicationContext.xml is a normal config file for a webapp.
You should also have this in your web.xml if using spring portlet MVC:
<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
In your portlet.xml I guess you have something like this to specify your portlets:
<portlet>
<portlet-name>sample</portlet-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Sample Portlet</title>
</portlet-info>
</portlet>
If you haven't already, see the spring portlet mvc reference documentation
Hope it helps.

#Service are constructed twice

I have a problem with my Spring application where my #Service classes are being created twice when the application starts. I know this is a problem with my configuration, as I've experienced it before, but what exactly am I doing wrong?
Is there anything fundamentally wrong with the way I've laid out my config, below? (I have omitted everything I deem to be irrelevant)
web.xml:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myapp-config.xml
/WEB-INF/myapp-security.xml
/WEB-INF/myapp-mvc.xml
</param-value>
</context-param>
<listener>
<listener-class>com.myapp.servlet.MyAppContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
myapp-servlet.xml
<context:component-scan base-package="com.myapp" annotation-config="true" />
<mvc:annotation-driven />
myapp-config.xml
<context:component-scan base-package="com.myapp" annotation-config="true" />
<context:annotation-config />
In addition to #GaryF's answer, there is a following beautiful solution for this problem (used in projects generated by Spring Roo):
myapp-config.xml
<!-- Load everything except #Controllers -->
<context:component-scan base-package="com.myapp">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation"/>
</context:component-scan>
myapp-servlet.xml
<!-- Load #Controllers only -->
<context:component-scan base-package="com.myapp" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation"/>
</context:component-scan>
EDIT:
Removing <context:component-scan> from myapp-config.xml means, that all your autodiscovered annotated beans are registered in DispatcherServlet's context (that is, the context loaded from myapp-servlet.xml).
However the recommended approach is to use servlet's context for presentation-specific things (such as controllers), and use the root context (myapp-config.xml) for the core services of your application. The solution above make this approach easy.
Regarding the practical considerations, when your core services are placed in servlet's application context, they can't be accessed from outside the scope of that servlet, for example, from another servlets (you may need to use another servlets to implement another access technologies) or filters (such as Spring Security filters). That's the reason for having core services in the root application context.
As an addition to the answer #axtavt gave, I’d like to give the matching Spring JavaConfig here.
In RootConfig.java:
#ComponentScan(basePackages = { "com.myapp" },
excludeFilters = #Filter({Controller.class, Configuration.class}))
In WebMvcConfig.java:
#ComponentScan(basePackages = { "com.myapp" },
useDefaultFilters = false, includeFilters = #Filter(Controller.class))
You're doing two separate component-scans over the same base-package. Remove one of them.

Splitting applicationContext to multiple files

What is the correct way to split Spring's configuration to multiple xml files?
At the moment I have
/WEB-INF/foo-servlet.xml
/WEB-INF/foo-service.xml
/WEB-INF/foo-persistence.xml
My web.xml has the following:
<servlet>
<description>Spring MVC Dispatcher Servlet</description>
<servlet-name>intrafest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/foo-*.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/foo-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
The actual questions:
Is this approach correct/best?
Do I really need to specify the config locations both in the DispatcherServlet AND the context-param sections?
What do I need to keep in mind to be able to reference beans defined in foo-servlet.xml from foo-service.xml? Does this have something to do with specifying contextConfigLocation in web.xml?
Update 1:
I'm using Spring framework 3.0. It's my understanding that I don't need to do resource importing like this:
<import resource="foo-services.xml"/>
Is this a correct assumption?
I find the following setup the easiest.
Use the default config file loading mechanism of DispatcherServlet:
The framework will, on initialization
of a DispatcherServlet, look for a
file named [servlet-name]-servlet.xml
in the WEB-INF directory of your web
application and create the beans
defined there (overriding the
definitions of any beans defined with
the same name in the global scope).
In your case, simply create a file intrafest-servlet.xml in the WEB-INF dir and don't need to specify anything specific information in web.xml.
In intrafest-servlet.xml file you can use import to compose your XML configuration.
<beans>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
<import resource="foo-services.xml"/>
<import resource="foo-persistence.xml"/>
</beans>
Note that the Spring team actually prefers to load multiple config files when creating the (Web)ApplicationContext. If you still want to do it this way, I think you don't need to specify both context parameters (context-param) and servlet initialization parameters (init-param). One of the two will do. You can also use commas to specify multiple config locations.
Mike Nereson has this to say on his blog at:
http://blog.codehangover.com/load-multiple-contexts-into-spring/
There are a couple of ways to do this.
1. web.xml contextConfigLocation
Your first option is to load them all into your Web application
context via the ContextConfigLocation element. You’re already going
to have your primary applicationContext here, assuming you’re writing
a web application. All you need to do is put some white space between
the declaration of the next context.
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value>
applicationContext1.xml
applicationContext2.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
The above uses carriage returns. Alternatively, yo could just put in a
space.
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value> applicationContext1.xml applicationContext2.xml </param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
2. applicationContext.xml import resource
Your other option is to just add your primary applicationContext.xml
to the web.xml and then use import statements in that primary context.
In applicationContext.xml you might have…
<!-- hibernate configuration and mappings -->
<import resource="applicationContext-hibernate.xml"/>
<!-- ldap -->
<import resource="applicationContext-ldap.xml"/>
<!-- aspects -->
<import resource="applicationContext-aspects.xml"/>
Which strategy should you use?
1. I always prefer to load up via web.xml.
Because , this allows me to keep all contexts isolated from each
other. With tests, we can load just the contexts that we need to run
those tests. This makes development more modular too as components
stay loosely coupled, so that in the future I can extract a package
or vertical layer and move it to its own module.
2. If you are loading contexts into a non-web application, I would use the import resource.
There are two types of contexts we are dealing with:
1: root context (parent context. Typically include all jdbc(ORM, Hibernate) initialisation and other spring security related configuration)
2: individual servlet context (child context.Typically Dispatcher Servlet Context and initialise all beans related to spring-mvc (controllers , URL Mapping etc)).
Here is an example of web.xml which includes multiple application context file
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd">
<display-name>Spring Web Application example</display-name>
<!-- Configurations for the root application context (parent context) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context -->
/WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context -->
</param-value>
</context-param>
<!-- Configurations for the DispatcherServlet application context (child context) -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/mvc/spring-mvc-servlet.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
</web-app>
#eljenso : intrafest-servlet.xml webapplication context xml will be used if the application uses SPRING WEB MVC.
Otherwise the #kosoant configuration is fine.
Simple example if you dont use SPRING WEB MVC, but want to utitlize SPRING IOC :
In web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
Then, your application-context.xml will contain: <import resource="foo-services.xml"/>
these import statements to load various application context files and put into main application-context.xml.
Thanks and hope this helps.
I'm the author of modular-spring-contexts.
This is a small utility library to allow a more modular organization of spring contexts than is achieved by using Composing XML-based configuration metadata. modular-spring-contexts works by defining modules, which are basically stand alone application contexts and allowing modules to import beans from other modules, which are exported ín their originating module.
The key points then are
control over dependencies between modules
control over which beans are exported and where they are used
reduced possibility of naming collisions of beans
A simple example would look like this:
File moduleDefinitions.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<module:module id="serverModule">
<module:config location="/serverModule.xml" />
</module:module>
<module:module id="clientModule">
<module:config location="/clientModule.xml" />
<module:requires module="serverModule" />
</module:module>
</beans>
File serverModule.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="serverSingleton" class="java.math.BigDecimal" scope="singleton">
<constructor-arg index="0" value="123.45" />
<meta key="exported" value="true"/>
</bean>
</beans>
File clientModule.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:module="http://www.gitlab.com/SpaceTrucker/modular-spring-contexts"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.gitlab.com/SpaceTrucker/modular-spring-contexts xsd/modular-spring-contexts.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<module:import id="importedSingleton" sourceModule="serverModule" sourceBean="serverSingleton" />
</beans>

Resources