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
Related
The method with #Scheduled annotation is executed twice (cron entry 0 */5 * * * *).
I have read that the problem is probably with context which is loaded twice.
My web.xml file looks like that.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/mvc-config.xml
</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
After I removed the listener from web.xml the scheduling was executed correcty once.
However there was another problem. When I visited the website there was error message.
Cannot be the problem with Dispatcher entry where mvc-config is occurred twice in web.xml?
What is correct path to my hhconfig.xml from web xml?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/hhconfig.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>hhconfig</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hhconfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is my screen from project tree
http://zapodaj.net/22e3adc3a776f.png.html
Your hhconfig.xml must be located directly in src/main/resources - currently it is under src/main/resources/META-INF. All files from src/main/resources will be added to <war>/WEB-INF/classes, thus they will be available on your web app's classpath.
You also need to change classpath:/hhconfig.xml to classpath:hhconfig.xml. Check Spring documentation for examples of resource strings.
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.
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.
Is there a way that a context can be loaded using web.xml in a Spring MVC application?
From the spring docs
Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web.xml and use a contextConfigLocation to set which context files to load.
The <context-param>:
<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>
You can then use the WebApplicationContext to get a handle on your beans.
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");
See http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html for more info
You can also specify context location relatively to current classpath, which may be preferable
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You can also load the context while defining the servlet itself (WebApplicationContext)
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
rather than (ApplicationContext)
<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>
or can do both together.
Drawback of just using WebApplicationContext is that it will load context only for this particular Spring entry point (DispatcherServlet) where as with above mentioned methods context will be loaded for multiple entry points (Eg. Webservice Servlet, REST servlet etc)
Context loaded by ContextLoaderListener will infact be a parent context to that loaded specifically for DisplacherServlet . So basically you can load all your business service, data access or repository beans in application context and separate out your controller, view resolver beans to WebApplicationContext.