Tweak of DispatcherServlet settings web.xml - need alternative BeanInjection approach - spring

I am trying to tweak the definition of my dispatcher servlet in my web.xml.
I had everything working perfectly using the settings below...
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
classpath:META-INF/spring/spring-application-context.xml
</param-value>
</context-param>
But now I am trying to tweak the settings to be the following and I get errors...
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/spring/spring-application-context.xml
</param-value>
</context-param>
The error I get is a null pointer exception for one of my filter classes because in that class I do the following...
#Autowired
#Qualifier("sessionRegistry")
private SessionRegistry sessionRegistry;
Where #Qualifier("sessionRegistry") is defined somewhere in classpath:META-INF/spring/spring-application-context.xml. So what I was doing was grabbing a bean defined in spring-application-context.xml using a qualifier but I can no longer do that with the new DispatcherServlet definition inside web.xml.
Can someone suggest to me an alternative method of injecting the sessionRegistry bean defined in my spring-application-context.xml that will work.
SessionRegistry is defined in spring-application-context.xml like this...
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
updated
I have a ContextLoaderListener defined in web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Here is a link to my full web.xml...
http://pastebin.com/fJXwGNgn
thanks

Related

The requested resource is not available on project launch spring mvc

Please why I am getting the requested resource is not found on project start up even though everything seems alright
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Please assist me!!!
You need to load the Spring context with org.springframework.web.context.ContextLoaderListener, not Log4jConfigListener (or try out Spring Boot)
See Loading context in Spring using web.xml

Spring not initializing session bean with requestcontextlistener

I am trying to decouple my webapp from enunciate and I have a web.xml as follows but when Spring 3 bootstraps I get:
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire method:.... Scope 'session' is not active for the current
thread; consider defining a scoped proxy for this bean if you intend
to refer to it from a singleton; nested exception is
java.lang.IllegalStateException: No thread-bound request found: Are
you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If
you are actually operating within a web request and still receive this
message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet: In this case, use
RequestContextListener or RequestContextFilter to expose the current
request.
The thing is I have a request context listener:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>org.springframework.security.filterChainProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>org.springframework.security.filterChainProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>jersey2</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.mypackage.web</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Trace</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>
org.mypackage.web.rest.SerializableProvider
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey2</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jersey2</servlet-name>
<url-pattern>/rest/myurl/session</url-pattern>
</servlet-mapping>
</web-app>
So why does Spring not boot properly? Could it be because its thread related and sessionbean to sessionbean?
According to error you can not autowire session scope class because your singleton classes loaded when application starts. In the beginning session does not exist.
You need to create proxy interface
ex : #Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)

application-config.xml vs mvc-config.xml in spring

Im a newbie to Spring and trying to understand the web.xml file.
I have created a new SPring MVC Maven project using STS,
I'm little bit confused between the application-config.xml vs mvc-config.xml file...
mvc-config.xml contains the servlet mappings but what information does the application-config file contains..
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Usually the mvc configuration(/WEB-INF/mvc-config.xml) contains the the beans that are needed by the controller layer (e.g. the controllers, view resolvers ...) The application configuration(classpath:spring/application-config.xml) is for the model layer (here you can define daos, services...)

Spring:Error while accessing scoped beans from jsp

I learnt how to access session scoped sping beans from a jsp but got the following error:
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? scoped beans from jsp
request.getSession().getAttribute("scopedTarget.otmSessionHolder")).getUserVO()
my web.xml:
<listener>
<description>Spring Context Listener</description>
<display-name>Spring Context Listener</display-name>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<display-name>Request Context Listener</display-name>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet>
<description>This Servlet intercepts all requests for this WebApplication</description>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans/web/**/*-Beans.xml</param-value>
</init-param>
<servlet>
<description>This Servlet intercepts all requests for this WebApplication</description>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans/web/**/*-Beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>This Servlet intercepts all RESTful requests for this Web Application</description>
<servlet-name>RESTfulSpringDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans/web/RESTful-Beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
But still I get this exception
I use sitemesh decorator for template.Have been going thru these forums,but to no avail.Kindly help me on this

How to add a Filter in Spring (with BlazeDS)

I want to add a filter to map a specific path in URL.
My server side used Spring 2.5.x, BlazeDS (servlet) with TomCat server.
So, my web.xml file is composed like that :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-main-config.xml
</param-value>
</context-param>
<filter>
<filter-name>FacebookOAuthFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>FacebookOAuthFilter</filter-name>
<url-pattern>/fbauth</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC Servlet (that will route HTTP requests to BlazeDS) -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-main-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
When I start my TomCat server, an exception is catched :
[BlazeDS][ERROR] [Configuration] MessageBroker failed to start: Exception: flex.messaging.config.ConfigurationException: MessageBroker already defined from MessageBrokerServlet with init parameter messageBrokerId = '_messageBroker'
at flex.messaging.MessageBroker.registerMessageBroker(MessageBroker.java:1916)
COuld you help me please ?
Thank you very much,
Anthony
I believe you are loading the incorrect configuration file here...
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-main-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
you have alreaded loaded /WEB-INF/spring-main-config.xml in the first few lines of the file
http://www.springbyexample.org/examples/simple-flex-webapp.html
This isn't really a Flex or BlazeDS issue, it's a more basic mis-configuration of Spring.
You're configured two separate Spring app-contexts, both with the same set of bean definitions (/WEB-INF/spring-main-config.xml).
The app-context defined by the <context-param> is the app-context associated with the webapp. The app-context defined by the ` is associated with the servlet.
Since you've given the same beans file to both, it'll instantiate and initialize the same set of beans twice, and the second time seems to be failing because the MessageBroker has already been defined.
You either need to break up your bean definitions into two sets, or just remove the first one, and just use the servlet context.

Resources