Trouble with hibernate and spring xml configuration - java

Hello guys i have been playing around with spring mvc and hibernate. But now i am getting an error when trying to connect both hibernate and spring with XML configuration. I get this error below:
03-Nov-2015 22:22:19.675 WARNING [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in ServletContext resource [/WEB-INF/services.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/services.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error accessing stax stream
I created my webapp folder in src/main. in my webapp folder i created WEB-INF folder. And WEB-INF folder contained files: service.xml, dispatcher-servlet.xml. Now for my hibernate.cfg.xml, it was placed in src/main folder structure. But when i try to add it in the service.xml upon loading the dispatcher, my hibernate.cfg.xml seems not to be found. Does anyone know why? This is where i am having trouble:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="" />
</bean>
I can't seem to find the value of configLocation. Has anyone encountered this issue.
Below is my web.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<display-name>Lab 2</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml, /WEB-INF/services.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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="edu.sjsu.cmpe275.lab2.controller" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> -->
</beans>
service.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="personService" class="edu.sjsu.cmpe275.lab2.service.PersonImpl" autowire="byName"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/Lab2" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="" />
</bean>
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:component-scan base-package="edu.sjsu.cmpe275.lab2.service" />
<!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> -->
</beans>

I fixed my error by placing hibernate.cfg.xmlin the root directory of WEB-INF. That is placed in webapp folder and solved my issue.

you have already defined "DataSource" bean, I guess you don't need to configLocation property.
you can refer below link for more options http://www.mkyong.com/spring/maven-spring-hibernate-mysql-example

Related

Spring not dinfing dispatchServlet

I am new to spring. And I have encounter an error on my spring application. It says :
GRAVE: Servlet [dispatcher] in web application [/web-customer-function] threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1309)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:546)
I have beean dealing with this for a long time now... It has been 5 days. And this is what I have done...
Deployment Asssembly > Add lib folder to path
Stop,clean or/and delete server
Rewrite entire code
Download the org.springframework.web.servlet.DispatcherServlet and try to run again.
I would appreciate the help to all if possible.
This is my Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>spring-mvc-crud-demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-crud-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And this part is my spring-crud.xml which are beans and configurations...
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false" />
<property name="user" value="springstudent" />
<property name="password" value="springstudent" />
<!-- these are connection pool properties for C3P0 -->
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.luv2code.springdemo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
</beans>
And there is a Controller class... Just in case ...
package com.luv2code.springdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/customer")
public class CustomerController {
#RequestMapping("/list")
public String listAll(Model model) {
return "list-customers";
}
}
I got the problem fixed...
Thank you all for the help it made my think again. So I refactor the code from zero checked the libraries and it ran smoothly. So the issue was with the libraries because after refactoring the code it did not work. But checking the libraries it worked.

Datasource bean definition in applicationContext.xml

I am building a Spring MVC Web application. I have created applicationContext.xml file. I am trying to connect to mysql database. But when I define datasource bean in applicationContext.xml the tomcat server starts and it says 404- resource not found.
If I remove all the beans from the applicationContext.xml the project runs properly.
What am I doing wrong? It is said that datasources should be defined in the applicationContext.xml but is there other way to define datasource?
Here is my applicationContext.xml file.
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.cinema.repository" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userDao" class="com.cinema.repository.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<context:property-placeholder location="jdbc.properties" />
</beans>
And this is my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<display-name>Cinema Booking</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<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>
</web-app>
Can you please point out what am I doing wrong here?
san your problem is most likely to be in <context:property-placeholder location="jdbc.properties" /> it should be like this <context:property-placeholder location="classpath:/jdbc.properties" /> i am assuming that the application can not start because there is an error in the listener (the listener can not find the jdbc.properties)i think that you have put jdbc.properties in your classpath because you probably wont put jdbc.properties in the web folder making it accessible to every one can access to your application

Spring mobile redirect 404 error

I'm building the mobile version of the site with different layout. I'm using Spring mobile+Thymeleaf. For testing purposes, I'm getting redirected from main site to mobile version. Here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<display-name>Student Accounting site</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>studentacc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>studentacc</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/m/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>deviceResolverRequestFilter</filter-name>
<filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>
</webapp>
And here's my servlet.xml
<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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.something.webversion"/>
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
<!--Thymeleaf beans init:START-->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>
<!--Thymeleaf beans init:END-->
<!--Other beans init:START-->
<mvc:interceptors>
<!-- Resolve the device which has generated the request -->
<bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
<!-- User's site preference -->
<bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
<!-- Redirects users to the device specific site -->
<bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="urlPath">
<constructor-arg value="/m" />
</bean>
</mvc:interceptors>
<bean id="liteDeviceDelegatingViewResolver" class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
<constructor-arg>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</constructor-arg>
<property name="mobilePrefix" value="mobile/" />
<property name="enableFallback" value="true" />
</bean>
<!--Other beans init:END-->
<mvc:resources mapping="/resources/*" location="resources/"/>
</beans>
And mine controller:
public class MainController {
private static final Logger logger = Logger.getLogger(MainController.class.getName());
#RequestMapping(value = "/")
public String HelloWorld(Model model,Device device){
logger.info("Device is "+device);
if(device.isNormal()){
return "hello";
}
return "index";
}
}
Mine webapp folder look like this:
WEB-INF---
|
pages---
|
mobile---
|
index.html
So, the redirecting work just fine,I'm getting redirected to mobile version. But page i'm redirected to is non existing(404). I' just can't see the error. Thank you in advance.
"rotate" the redirect to url to sitename:8080/studentacc/m" because it is likely that "studentacc" is the name of your application in the application server, and therefore it must be the first part.
From a very short look to the API, I would try to use the SiteSwitcherHandlerInterceptor.urlPath(String mobilePath, String rootPath) method with the additional parameter for the rootPath
<bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="urlPath">
<constructor-arg value="/m" />
<constructor-arg value="/studentacc" />
</bean>

Spring webmvc chaining viewresolvers gives BeanNotOfRequiredTypeException

I have a simple (sample) web application that i have a problem with if i try chaining multiple viewresolvers. When my application is configured in the way described below, trying to access http://localhost:8080/Library/spring/initApplication will throw the error described below.
It seems as though the InitApplication is erroneously being picked up by the wrong view resolver. If i change the order of the view resolvers everything works, but im still wondering why exactly this error occurs because i cant pinpoint why it happens. Im guessing a configuration issue?
26027 ["http-bio-8080"-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'initApplication' must be of type [org.springframework.web.servlet.View], but was actually of type [library.controller.InitApplication]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:349)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:266)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
at org.springframework.web.servlet.view.ResourceBundleViewResolver.loadView(ResourceBundleViewResolver.java:196)
at org.springframework.web.servlet.view.AbstractCachingViewResolver.createView(AbstractCachingViewResolver.java:158)
at org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:77)
at org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1078)
Details of the important files:
InitApplication.java
#Controller
public class InitApplication {
#RequestMapping(value = "/initApplication", method = RequestMethod.GET)
etc.......
web.xml
<?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" 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">
<display-name>testwebproj</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
</web-app>
springmvc-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:context="http://www.springframework.org/schema/context"
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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/library" />
<property name="username" value="root" />
<!-- <property name="password" value="" /> -->
</bean>
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<bean id="pdfViewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="order" value="1" />
<property name="basename" value="views" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="library"></context:component-scan>
</beans>
views.properties
pdfPrinter.(class)=library.controller.DummyController
It is certainly because your are mapping your view with a Controller class mate!
pdfPrinter.(class)= you should indicate a View class here!
For example to map a Controller to a jsp View, you will need to proceed like this:
login.(class)=org.springframework.web.servlet.view.JstlView
login.url=/vues/en/login.jsp
have you tried renaming your bean? You may be clobbering an internal bean used by spring

Mixing *-servlet.xml with applicationContext.xml

I have an existing Spring application that does some server side processing. I am trying to create a webapp for this particular application and chose SpringMVC to serve my purpose.
I created a display controller as follows:
#Controller
#RequestMapping("/items")
public class ItemDisplayController {
private static final Logger LOGGER = Logger.getLogger(ItemDisplayController.class);
private static final String ITEMS_REDIRECT = "redirect:/item/items";
#Autowired
private ItemDisplay itemDisplay;
#RequestMapping
public String listItems(ModelMap model) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("List all items");
}
List<ItemDetail> itemDetails = itemDisplay.getAllItems();
model.addAttribute("itemDetails",itemDetails);
return "items";
}
}
I already have an applicationContext file with the following definitions:
<?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.xsd">
<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:item.properties</value>
<value>file:#{systemEnvironment['ITEM_HOME']}/item.properties</value>
<value>file:#{systemProperties['ITEM_HOME']}/item.properties</value>
</array>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
<bean id="itemDisplay" class="com.acme.itemDisplayImpl">
<property name="itemDisplayDAO" ref="jdbcItemDisplayDAO"/>
</bean>
<bean id="jdbcItemDisplayDAO" class="com.acme.database.dao.JdbcItemDisplayDAO">
<property name="dataSource" ref = "dataSource"/>
</bean>
<bean id="realDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{props['itemds.jdbc.driver']}"/>
<property name="url"><value><![CDATA[#{props['itemds.jdbc.url']}]]></value></property>
<property name="username" value="#{props['itemds.username']}"/>
<property name="password" value="#{props['itemds.password']}"/>
<property name="testOnBorrow" value="#{props['itemds.test.on.borrow']}"/>
<property name="testWhileIdle" value="#{props['itemds.test.while.idle']}"/>
<property name="poolPreparedStatements" value="#{props['itemds.pool.prepared.statements']}"/>
<property name="validationQuery" value="#{props['itemds.validation.query']}"/>
<property name="validationQueryTimeout" value="#{props['itemds.validation.query.timeout']}"/>
<property name="timeBetweenEvictionRunsMillis" value="#{props['itemds.time.between.eviction.runs.millis']}"/>
<property name="maxActive" value="#{props['itemds.max.active']}"/>
<property name="maxIdle" value="#{props['itemds.max.idle']}"/>
<property name="initialSize" value="#{props['itemds.initial.size']}"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="realDataSource"/>
</bean>
</beans>
In my *servlet.xml, I have defined the ViewResolver as follows:
<?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:annotation-config/>
<context:component-scan base-package="com.acme.item"/>
<bean id="primaryViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="order" value="1"/>
</bean>
</beans>
And in the web.xml, I have the context-param defined:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
xmlns="http://java.sun.com/xml/ns/javaee">
<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>
<servlet>
<servlet-name>items</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>items</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
When I run my application, the itemDisplay is not wired up and on the debugger it is showing up with a null value.
Can somebody point out what I may be doing wrong here? In an ideal world, I would assume that annotating the itemDisplay with #Autowired in the controller would help resolve the implementation of the interface.
Only thing I can think of is, ContextLoaderListener does not complain if the applicationContext.xml is not found, Let's try this add classpath*:applicationContext.xml also make sure applicationContext.xml is run time class path of the server
Here is an excellent article that provides a good insight into spring classpath resource.
Your files looks up fine at quick glance. As long as there is only one bean with of ItemDisplay type in your context it should be autowired by type, otherwise you should get error in log file.
One small thing I noticed, which is probably unrelated to your particular issue, is that you map your 'items' controller to all files in web.xml. Usually you would want it to map to particular type of files only - say *.htm so it won't get called for resources such as images etc.
Not sure if this is a typo in your applicationContext file :
<bean id="itemDisplay" class="com.acme.itemDisplayImpl"/>
Shouldn't the class be com.acme.item.DisplayImpl ?. If the name of class is indeed itemDisplayImpl, then you need to change the component-scan element in *servlet.xml to include the correct package.

Categories

Resources