Eclipse Spring MVC Project Configuration Files - spring

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>

Related

Spring Session and Spring Security (3.2.X RELEASE)

We have a web application built upon Spring Framework 3.2.x (3.2.12-RELEASE at moment) and Spring Security 3.2.x (i.e 3.2.5-RELEASE)
Security is implemented with classical Spring approach. In web.xml we load ApplicationContext and springSecurityFilterChain
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/applicationContext*.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>
As of a change in requirements, we are trying to investigate on how to permit different HttpSessions per tab (or window) within the same browser: in this scenario the same physical user can log in the application using two separate app-users.
I can understand that this is not possibile using JSESSIONID cookie approach. I'm trying using Spring Session (Spring Session and Spring Security
and Spring Session Multiple Session) but i can't manage doing it, having difficulties mixing my XML configuration with the JavaConfig approach shown in links before, Tomcat does not even start with a lot of errors.
I'm new with mixing XML and JavaConfig approch, so..can anyone give me an hint in how to proceed with Spring Session?
Are there other ways to fulfill my requirement?
Spring Session project actually contains a few sample projects that demonstrate usage with Spring XML config. Look for the samples with -xml suffix in the sample name.
Generally speaking, what you need to do is manually register appropriate #Configuration class as a #Bean. For example, if you want to use Spring Session backed by Redis, register RedisHttpSessionConfiguration. You'll find the correct #Configuration class to use by looking at appropriate #Enable*HttpSession annotation (in this case #EnableRedisHttpSession) and determining which class is imported via #Import annotation.

log4j2 - configuration file found

I am using servlet 2.5 and i followed the instructions to set up the listeners and filters, but still my webapp complains that the configuration file cannot be found. My listener, context param,filter and filter mappings are all defined as the first values in their respective locations in my web.xml. I made sure log4j-web-2.2.jar is in my classpath as well. Are there other options to load them from a spring bean? I would like to load a different config file based on a value in catalina.properties.
Can somebody please advice?
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:*${sys:log4j2.xml.name}</param-value>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
After a lot of fiddling, i learned that with the classpath notation we cannot use system variables. So i went with the following approach. This basically allows us to provide a different file name based on our environment. This variable has to be specified in catalina.properties.
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/config/${sys:log4j.fileName}</param-value>
If your config file is named log4j2.xml and is located in the WEB-INF/ directory then you don't need to specify the log4jConfiguration context parameter in your web.xml.

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>

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.

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.

Resources