There is a web application based on Spring 3.2. Here is how it's configured in web.xml:
<servlet>
<servlet-name>MY-REST-API</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MY-REST-API</servlet-name>
<url-pattern>/foo/*</url-pattern>
<url-pattern>/bar/*</url-pattern>
</servlet-mapping>
Spring context is configured by XML file MY-REST-API-servlet.xml placed in a classpath. Nothing unusual as you can see.
There is a code which is not in ApplicationContext. I'm looking for a way to give this code an ability to reach some beans in my Spring MVC app. AFAIK ContextSingletonBeanFactoryLocator can be used to solve such problems. But small investigation I've done with dubugger shows that Spring MVC doesn't use it by default while loading a context. None of getInstance() methods in ContextSingletonBeanFactoryLocator was called as Spring MVC bootstrapping. Hence a context received through ContextSingletonBeanFactoryLocator will be not the same as one loaded during bootstrap. I think the problem will be resolved if I make Spring MVC to use ContextSingletonBeanFactoryLocator in a context loading process.
Is there a way to configure Spring MVC to use ContextSingletonBeanFactoryLocator during bootstrap?
change your web.xml file add context-param and specify the xml file as describe below
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:MY-REST-API-servlet.xml
</param-value>
</context-param>
<servlet>
<servlet-name>MY-REST-API</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>classpath:MY-REST-API-servlet.xml </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Related
There are many Qs and As on this matter but I have not seen an answer to why my project works. I started it out following a tutorial so it just worked. This is the declaration of my Spring Dispatcher Servlet in web.xml:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Then I have my servlet definition in a file in the same folder called dispatcher-servlet.xml. But I am not declaring in the web.xml that the definition is to be found in that particular file. How can the web app know that this is the correct file?
I have seen other default names of the file in answers but none like mine. Is dispatcher-servlet.xml a default name that Spring knows about and can find?
From the Spring MVC documentation:
Upon initialization of a DispatcherServlet, Spring MVC looks for a
file named [servlet-name]-servlet.xml in the WEB-INF directory of your
web application and creates the beans defined there, overriding the
definitions of any beans defined with the same name in the global
scope.
Since your servlet name is 'dispatcher' Spring looks for the file dispatcher-servlet.xml in the default location of your WEB-INF folder.
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
when I am removing listener part from web.xml my project is running fine. Please let me know why?
<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_2_5.xsd"
version="2.5">
<display-name>migration</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
This is a spring web application in eclipse.
ContextLoaderListener is optional. If all you're beans are in the (child) web context (DispatcherServlet) you can remove it.
How it works basically, Spring load a root WebApplicationContext via Spring’s ContextLoaderListener and a child WebApplicationContext via Spring’s DispatcherServlet. This results in a parent-child context hierarchy.
If you don't need anything in the root hierarchie then just keep the DispatcherServlet.
Further reading :
Role/Purpose of ContextLoaderListener in Spring?
ContextLoaderListener or not?
and of course http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
Only if you have two config xml files. Say, SerivesContext.xml, ContextDAO.xml
If you have configured everything in one spring config file you don't need the
ContextLoaderListener, just the dispatcher servlet is sufficient.
If you use different ones , you can configure it as ,
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,WEB-INF/spring-security.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In Spring Web Applications, there are two types of container, each of which is configured and initialized differently. One is the Application Context and the other is the WebApplicationContext.
Purpose of ContextLoaderListener :
ContextLoaderListener is used to initialize Application Context. In this case, you will tell Spring to load all *-.context.xml files from the classpath. There is only one application context per application.
So you can configure this in web.xml as
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
Purpose of contextConfigLocation :
On the other hand, you have WebApplicationContext. WebApplicationContext is the child context of the Application Context. Each DispatcherServlet defined in a Spring web application will have an associated WebApplicationContext. You can initialize the WebApplicationContext as
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
NOTE : Here name of the XML must be like <servlet name>-servlet.xml. In our case name of servlet is dispatcher therefore, name of XML file is dispatcher-servlet.xml.
I hope this clears you when to use what & why.
How to stop loading context twice in spring 2.5 application?
In web.xml I having following configuration setting while loading spring based application, I m also using "CRON" scheduler to schedule the notification mail based on the "CRON" expression value, But notification triggering twice, but same case for simple trigger it is working fine.
<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>
<servlet>
<servlet-name>dummyProject</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Please find the above setting in web.xml and let me know the solution ,if anybody knows
I am new to Spring MVC. I have a web application. I have the following configuration:
<welcome-file-list>
<welcome-file>list.html</welcome-file>
</welcome-file-list>
<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>
Do I need to add the following line to the web.xml file?
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Yes you need to add ContextLoaderListener in web.xml,
only if you want to load other Spring context xml files as well while loading the app
and you can specify them as
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
Only if you have two config xml files. One with Services / DAOs and another with Controller. If you have configured everything in one spring config file you don't need the ContextLoaderListener, just the dispatcher servlet is sufficient.
It is recommended to split the config into two and use the ContextLoaderListener to create the root application context and the dispatcher servlet to create the web layer application context.
It is optional, you don't really need it just for Spring MVC (DispatcherServlet will do). But adding Spring security to your Spring MVC must be done with
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Just one remark, if using ContextLoaderListener you will have to add DelegatingFilterProxy:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>/admin</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
in your web.xml as well. Sorry for being four years too late. Cheers
This maybe a little advanced, in my application which is an enterprise app, they construct their own listener class and put in the web.xml. On start up, this customized listener will scan the app to gather all info including resources, external connections, server info ip, jars, etc. The info is accessible in a web page.
I just started with Spring Web MVC. I'm trying to avoid file extenstions in the url. How can i do this? (I'm using Spring 2.5.x)
Bean:
<bean name="/hello.htm" class="springapp.web.HelloController"/>
I want it to be:
<bean name="/hello" class="springapp.web.HelloController"/>
I cannot get it to work. Any ideas?
Edit:
Url-mapping
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
I have tried changing the url-pattern with no luck (* and /*).
In 3.0, / seems to work. That is...
<url-pattern>/</url-pattern>
As far as I know you can't do this if you're using JSP's as your view for controllers.
Because when you pass a model to a JSP, Spring MVC internally performs a 'forward' to the URL of the JSP. If you use <url-pattern>/*</url-pattern> then this forward will also be handled by your DispatcherServlet and not by your JSP view.
What you can do is use <url-pattern>/something</url-pattern> and have your JSP's in a different directory
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then you need to register your urls to be handled by a particular controller. See the following
http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
In Spring 3.2 at least, the accepted answer above is very nearly but not quite what's needed. The web.xml bit below just worked for me, and I'm adding it to the thread here for reference of whoever googles this next...
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Try first:
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
If that doesn't work then problem is somewhere else. Is your Apache set up to forward those urls to Tomcat? Something like:
JkMount /hello worker1
Have you tried <url-pattern>/*</url-pattern>
in the servlet mapping and <bean name="/hello" .../> ?