spring 2.5 - Move /WEB-INF/applicationContext.xml to resources/META-INF - spring

I've recently completed a spring mvc web application deployed as a war file built with maven that looked like this...
>MyApp
>src
>main
>java
>resources
>META-INF
>spring
>applicationContext.xml
>webapp
>WEB-INF
Now I'm trying to create a non-maven spring mvc application and I have this structure working...
>MyApp
>src
>WebContent
>WEB-INF
>applicationContext.xml
For this non-maven project I would like to move applicationContext.xml into a better location like I did in my maven project. I spent a lot of time playing around with it this morning but cannot find out how to do it successfully.
Can someone please give me a suggestion of how to best do it? i.e. If there should be a resources folder then where can I put it in my project structure and place the aplpicationContext.xml etc.
In my web.xml the location is specified as ....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Ideally after the change in location of my applicationContext.xml my web.xml would like similar to this.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/spring/spring-application-context.xml
</param-value>
</context-param>
I am using spring 2.5 because I'm restricted by a very old version on websphere.
thanks

Related

Eclipse Spring MVC Project Configuration Files

I have created a Spring MVC project through eclipse. I believe I used some plugins to generate the project directory. I find here there configuration files.
web.xml
root-context.xml
servlet-context.xml
I am kinda of familiar with Spring MVC & its dependency injection. However I have problems understanding the last two configuration files (root-context & servlet-context).
What kind of configurations do they contain?
Also in may online examples I see mvc-dispatcher-servlet.xml. Why did eclipse not generate this xml file in my project?
[IMPORTANT] I wanted to set up strong security and user authentication for my web app. I have been following online tutorials again and they all create a seperate
xml file named spring-security.xml and add the namespace information to that file. Does it suffice if I just create this file and add the name space information? I mean
dont' I need to import this file to a main file that is scanned by Spring framework?
How do I define and where do I put spring application context.xml file and start wiring the dependencies together? Also if I define everything (all dependencies here) how is this file picked up by the framework?
Thanks,
Configuration Files
If you check your web.xml you will find both of root-context.xml and servlet-context.xml files being referred here. One used by Dispatcher Servlet and other by Context Loader Listenter. You can name your files to whatever unless they are being refereed in web.xml
Eclipse Not generating files
Every editor works its own way. some may generate full fledged project/app with both DispatcherServlet and ContextLoaderListner configured or some with only DispatcherServlet ( with minimal configutaion). Check Spring Roo it starts with basic and gives you the flexibility to generate a strong app.
mvc-dispatcher-servlet.xml is not there
Some of the thing in spring projects are convention based, for example if you are not providing any file to your DispatcherServlet in web.xml spring looks for mvc-dispatcher-servlet.xml file, and if you have provided it won't look for.
Spring Security
To Configure Spring Security you need to provide at least some configuration. But the question is where. You need to add this configuration to your web.xml only. and Hence no need to import this to any other file.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener- class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>
<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>
Where to define application context.xml
Just define it any where, configure beans in it.
You can add this file as follows:
a) Either Import this into some other configuration file like root-context.xml or servlet-context.xml
as <import resource="application-context.xml"/>
b) Add this into web.xml with ContextLoaderListner as context param
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/application-context*.xml
classpath*:META-INF/spring/abc*.xml
</param-value>
</context-param>

load spring context

I use intelliJ Idea 10.5. when i use context-param for for spring context and run project Tomcat show me blank page!! when I disable it, page show!
<context-param>
<param-name>contextLocation</param-name>
<param-value>classpath:context/applicationContext-dao.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You need to enable log4j and see the exception Spring is throwing when you start tomcat.. Also make sure that the context file is in the war file (on the location - WEB-INF/classes/context/applicationContext-dao.xml
Ian.

spring application contexts

i have a maven project that consist of three modules .
model , cor , web .
both model and core have their own application contexts , and i want to import them in my web project . i added this part to web.xml :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-*.xml
</param-value>
</context-param>
when i deploy the project on tomcat , spring cant find any of application contexts .
i have to note that i dont get any exceptions .
but when i copy all application contextes on somewhere like WEB-INF/spring
and point contextConfigLocation to it , everything works.
what is the problem?
I would advise another thing. Use <import ... /> in your main applicationContext.xml to import the two additional ones.

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.

Spring XML files outside WEB-INF

I'm working in a multi-module maven2 project using Spring and GWT. Let's say I have two subprojects: service and presentation. "service" contains the actual implementations of the services and "presentation" is in charge of wrapping them for GWT.
I have successfully loaded the Spring XML files that are in the WEB-INF directory (the same as web.xml), but I'm having problems with those XML files that should be loaded from the other modules.
My web.xml looks like this:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring-presentation.xml, classpath:/spring-services.xml
</param-value>
</context-param>
Here spring-presentation.xml is in the presentation project and spring-services.xml is in the service project.
The spring-services.xml file is not being loaded. Am I missing something?
I am guessing that the presentation project is compiled as WAR and the services project is compiled as a JAR. Also that the presentation project has dependency on the services project.
In such case, include the spring-services.xml file as a resource in the services project's pom so that it goes in its jar and since the presentation project has a dependency on the services project, the services jar will be put in the /WEB-INF/lib folder and then Spring will be able to load it from the classpath.
If the value starts with classpath: it won't look in WEB-INF/ folder it will look in classpath, which is WEB-INF/classes and direct contents of all the jar files under WEB-INF/lib/.
To make it look in WEB-INF/ folder you need to specify the value like this:
<param-value>
/WEB-INF/spring-presentation.xml
/WEB-INF/spring-services.xml
</param-value>
(you can put them on one line separated by comma, but I like it that way)
use
<import resource="classpath:spring-services.xml" />
at the top of spring-presentation.xml instead of referring to it from the web.xml

Resources