Spring framework configuration - spring

It has been 3 days since I started learning about Spring Framework and trying to implement a RESTful web service with MongoDB and Spring Framework. I am still at the beginning and trying to understand the configuration of Spring Framework. When I start my project and hit the desired URL it is not working. I have also upload my project to github (here is the url)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="WebApp_ID"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example #Controller and #Service. Make sure to set the correct base-package-->
<context:component-scan base-package="shoponway.webservice" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<!-- Loads MongoDB configuraton -->
<import resource="mongo-config.xml" />
</beans>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
and my controller
#Controller
public class PersonController {
protected Logger logger = Logger.getLogger(PersonController.class);
#Resource(name = "personService")
private PersonService personService;
#RequestMapping(value = "/allpersons", method = RequestMethod.GET)
public String getAllPersons(Model model){
model.addAttribute("persons", personService.getAllPersons());
return "personspage";
}
}
The error I am getting
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4888)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

There are multiple problems in this example, which can be corrected as follows
add #Service anotation to your shoponway.webservice.services.PersonService class, you need to add #Service for making it a proper candidate for injection.
Your 'mongo-config.xml' is pointing to wrong mongo repository locations edit it to
<mongo:repositories base-package="shoponway.webservice.services" />
from
<mongo:repositories base-package="org.krams.tutorial.repositories" />
Mongo template should refer to following bean <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> not with class="org.springframework.data.document.mongodb.MongoTemplate"
4) replace mongo-config.xml content with
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mongo:mongo host="localhost" port="27017" />
<mongo:db-factory dbname="shoponwaydb" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
`
Once your correct these , you will be able to run your example easily:

Related

HTTP Status 404 - Not Found in Spring 3.2.7

I am using Spring 3.2.7, I have Login.jsp file which takes the input and should display the output on hello.jsp, it takes the input but does not show an output instead it throws an error as HTTP Status 404 - Not Found. Here is the code:
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String password = request.getParameter("password");
if (password.equals("admin")) {
String message = "Hello " + name;
return new ModelAndView("hellopage", "message", message);
} else {
return new ModelAndView("errorpage", "message", "Sorry Username or Password Error");
}
}
}
Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="mypack" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
After observing your Dispatcher-servlet.xml, I can see you are missing <mvc:annotation-driven/>
Your Updated code will look like below
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="mypack" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
Explanation
<mvc:annotation-driven/> means that you can define spring beans
dependencies without actually having to specify a bunch of elements in
xml or implement an interface or extend a base class.Means #Controller
tells spring that the the class specified contains methods that will
handle http requests without you having to implement the Controller
interface or extend a subclass that implements controller.
<context:component-scan base-package="mypack /> tells spring that it
should search the class path for all the classes under mypack and
look at each class to see if it has a #Controller, or #Repository, or
#Service, or #Component and if it does then Spring will register the
class with the bean factory as if you have defined in in the xml configuration files
No, I made changes in Dispatcher-servlet.xml as:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
">
<context:component-scan base-package="mypack" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
still am getting an error as "Status 404 - Not Found" when i submit the form.
Form:
<form action="hello.htm" method="post">
Name: <input type="text" name="name" /><br>
Password: <input type="password" name="password" />
<input type="submit" value="Login" />
</form>
Here is the Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<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>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>

Getting ClassNotFoundException while setting up Spring 4.0 Simple project Setup

I'm setting up a simple project with Spring 4. I'm not using Maven. Instead I've simply added the Spring Dependencies by adding the Jars to the classpath.
I'm using RAD 8.5 and Websphere 8.5 for this project.
When I am trying to start up the project, it is giving error:
class java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
I've provided the following in the web.xml:
<servlet>
<display-name>Controller</display-name>
<servlet-name>Controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
My Controller-servlet.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.spring4.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
My Controller class looks like:
#Controller
public class HelloSpring4Controller {
#RequestMapping("/hello")
public ModelAndView sayHello() {
String message = "Welcome to Spring 4.0 !!! ";
return new ModelAndView("welcome", "message", message);
}
}
I have a welcome.jsp under WEB-INF/view where I'm fetching the value put in message Object.
Can someone let me know where I'm going wrong?
Thanks,
Sayantani

Spring mvc resources not works

I created ,with Eclipse Luna, a simple Spring MVC Project but it doesn't works if i add mvc:resources in springConfig-servlet.xml. I have this errors in Tomcat:
PM org.springframework.web.servlet.PageNotFound noHandlerFound
Avvertenza: No mapping found for HTTP request with URI [/SpringStore/] in DispatcherServlet with name 'springConfig'
Where am I doing wrong?Thanks
This is the structure of my project
Web.xml code
<?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" id="WebApp_ID" version="3.0">
<display-name>SpringStore</display-name>
<servlet>
<servlet-name>springConfig</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springConfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springConfig-Servlet code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="controller"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>
HomeController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class HomeController {
#RequestMapping(value="/",method=RequestMethod.GET)
public String welcome(){
return "index";
}//welcome
}
The contents of the file index.jsp is very long and I don't write it here.
I think you are missing below entries in your springConfig-Servlet.xml file.
<mvc:default-servlet-handler />
<mvc:annotation-driven />

404 Returnred for index.htm in Spring MVC

I am getting 404 for index.htm. As you can see that index.htm is registered in the server.
Server log
[0m[0m10:00:57,643 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (ServerService Thread Pool -- 201) Mapped URL path [/index.htm] onto handler 'indexController'
[0m[0m10:00:57,644 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (ServerService Thread Pool -- 201) Root mapping to handler 'indexController'
...
[0m[0m10:07:31,827 INFO [stdout] (http-/0.0.0.0:8080-1) Error message=404 returned for /EAFUIWeb/index.htm with message Not Found : [Fri Sep 26 10:07:31 ICT 2014]Not Found
Controller
#Controller
public class IndexController {
#RequestMapping(value={"/index.htm","/"}, method = RequestMethod.GET)
public ModelAndView initGET(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
}
#RequestMapping(value={"/index.htm","/"}, method=RequestMethod.POST)
public ModelAndView loadEntity(HttpServletRequest request, HttpServletResponse response) throws Exception {
..
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
...
</web-app>
index.jsp
<c:redirect url="/index.htm" />
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<sec:global-method-security
pre-post-annotations="enabled" proxy-target-class="true" />
<mvc:annotation-driven />
<mvc:resources mapping="/config/**" location="/config/" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/tmpls/**" location="/tmpls/" />
<mvc:resources mapping="/media/**"
location="file:${user.home}/uploads/avatar/" />
<context:component-scan base-package="com.bara.j2ee.pattern.control.spring,com.master.util" />
<mvc:default-servlet-handler/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<context:property-placeholder location="classpath:ad.properties"
ignore-unresolvable="true" />
Is there any configuration problem? How can I fix 404 for index.htm
To solve those 404 problems, you need to understand how Spring finds and loads your JSPs.
In a short description...
When you call, in this case, /index.htm, DispatchServlet will look for a method that has /index.htm mapping and call that method. The method will return something. In this case, you are supposed to return a ModelAndView. something like:
modelAndView.setViewName("myPage");
return modelAndView;
Then Spring comes to list of ViewResolvers and by the order, it will check where it can find "myPage".
Again, in your case, as you only have one, it will look for /WEB_INF/jsp/myPage.jsp (p:prefix="/WEB-INF/jsp/" p:suffix=".jsp").
Your problem seems to be that Spring can't find your page (which I don't know as you didn't post your initGET method body.
Make sure that, whatever you are setting/returning there can be found /WEB_INF/jsp/ and ends with .jsp
Thanks all. I have figured it out. It was multiple viewresolver issue. I have no index.jsp file but another jsp servces /config/index.jsp and I have added in spring-view.xml. As per mkyong, this order should be preserved.
<bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/springapp-view.xml</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
spring-view.xml
<bean id="index" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/config/index.jsp" />
</bean>
Reference:
http://www.mkyong.com/spring-mvc/configure-multiple-view-resolvers-priority-in-spring-mvc/
To solve this answer you need to change your / to /*.htm
i think this is the problem.

property place holder bean in application-context.xml spring

I have a Spring MVC application where my web.xml looks like below:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>Test</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Now my application-context.xml looks as below:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/config/prop.properties</value>
</property>
</bean>
</beans>
And mvc-dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.test"></context:component-scan>
</beans>
Now in my controller I’m trying to use a property from property file using #Value as mentioned in following code.
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class TestController {
#Value("${message}")
String message;
#RequestMapping(value = "/")
public String test() {
String message = "Hello World, Spring 3.0!";
System.out.println(this.message);
return "page";
}
}
But property is not being loaded in the variable. Is there anything wrong with configuration. Also I move property place holder bean from application-context.xml to mvc-dispatcher-servlet.xml then I am not able to user property value in application-context.xml, say to create a datasource.
You could create a Properties bean using
<util:properties id="myProps" location="WEB-INF/config/prop.properties"/>
and then inject the value as
#Value("#{myProps['message']}")
this should work either in the root context or in the mvc context.

Resources