我对此并不陌生,正在尝试创建一个Spring5 MVC / Hibernate5 Web应用程序,但是当我启动Tomcat时出现以下错误:
最初的问题与事务管理器有关,在添加事务管理器bean定义之前,该应用程序启动正常,但失败并显示一条错误消息,提示它无法创建事务线程。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController' defined in ServletContext resource [/WEB-INF/spring/store.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy210 implementing com.store.service.CustomerService,java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.store.service.CustomerServiceImpl' for property 'customerService'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy210 implementing com.store.service.CustomerService,java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.store.service.CustomerServiceImpl' for property 'customerService': no matching editors or conversion strategy found
我的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">
<description>
Configuration file for the Store Application
</description>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/store-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>store</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>store</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的Store-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"
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">
<context:component-scan base-package = "com.store.controller" />
<context:annotation-config/>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<import resource="/spring/store.xml" />
<import resource="/hibernate/hibernate-configuration.xml" />
</beans>
我的休眠配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<beans:property name="url" value="jdbc:mariadb://localhost:3306/store" />
<beans:property name="username" value="keeper" />
<beans:property name="password" value="keeper123" />
</beans:bean>
<!-- Hibernate 5 SessionFactory Bean definition -->
<beans:bean id="hibernate5AnnotatedSessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.store.vo.Make</beans:value>
<beans:value>com.store.vo.Model</beans:value>
<beans:value>com.store.vo.Car</beans:value>
<beans:value>com.store.vo.Customer</beans:value>
<beans:value>com.store.vo.CustomerCar</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans:beans>
答案 0 :(得分:0)
我想在您的CustomerController
类中,您不是注入接口,而是注入了CustomerService
的实现,如下所示:
public class CustomerController {
@Autowired
private CustomerServiceImpl customerService;
}
具体的类是代理,因此您应该将注入点指定为接口:
public class CustomerController {
@Autowired
private CustomerService customerService;
}
答案 1 :(得分:0)
错误是我将具体类注入到Service类中,但是Service类还具有具体类而不是接口的设置器