Configuring servlet for using spring - java

How to configure my web application in Eclipse (based on Servlets and deployed to Tomcat) to use Spring framework. I need only IoC container (Beans only and ApplicationContext), not Spring MVC. How to configure web.xml for this?
Regards

One way is to put this in your web.xml:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
and have applicationContext.xml in WEB-INF, or have its location configured this way (in web.xml again)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>

Related

Order or loading within contextConfigLocation in web.xml of Spring Servlet project

In a spring-servlet web application, if there are multiple context xmls under contextConfigLocation, is it guaranteed that these context files will be loaded in the same order in which they are specified?
Sample web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
a.xml
b.xml
c.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
In the above sample web.xml, is it guaranteed that the beans in a.xml will be loaded before loading beans in b.xml, provided there is no other dependency(through injection or "depends-on") specified?

Spring security config, No child element is expected at this point.Invalid content was found

I am trying to add the security-config.xml file and in web.xml by using ContextLoaderListener, but after trying to add the <param-value> for security-config.xml got the error says:
Invalid content was found starting with element 'param-value'. No
child element is expected at this point.
The jpaContext.xml was used for JPA configuration, so I cannot remove it. What should I do at this point?
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/jpaContext.xml</param-value>
<param-value>/WEB-INF/config/security-config.xml</param-value>
</context-param>
ContextLoaderListener supports having multiple contextConfigLocation. Each context configuration file should be separated by spaces (new line or space). See an example below:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/jpaContext.xml /WEB-INF/config/security-config.xml</param-value>
</context-param>
The error you are experiencing is caused by having multiple <param-value> tags under <context-param>.
Use comma :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/jpaContext.xml, /WEB-INF/config/security-config.xml
</param-value>
</context-param>

spring 2.5 which loading context twice

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

Adding ContextLoaderListener to web.xml in Spring MVC

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.

Loading context in Spring using web.xml

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.

Categories