spring application contexts - spring

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.

Related

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

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

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>

Spring: link between WebApplicationContext and ApplicationContext?

I am working on a Spring application. I started from creating a small java app using spring. Later, it became necessary to add a web interface. I decided to use Spring MVC. Now I am confused. In my web.xml I have
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
where mvc-dispacher-servlet.xml contains beans necessary for web logic while applicationContext.xml contains beans performing some specific operations. My question is: Are beans in these files going to be aware of each other? Is it going to be a one big container which includes beans from both config files? or these containers are separate?
Yes it will be in one context which will be loaded from the web application context. Its the same as you would do when using the application context and passing in multiple files to it.

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