问题描述
- springMVC报错,请教大神!
-
最近在学习springMVC,搭建好框架后,报出了如下错误,求大神指教:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userManagerBase' defined in file [F:Softwareapache-tomcat-7.0.65webappsspringWEB-INFclassescommuaaxmlspring-user.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'uersDao' of bean class [comm.buaa.service.impl.UserService]: Bean property 'uersDao' is not writable or has an invalid setter method. Did you mean 'usersDao'?
我的项目结构:
spring-hibernate.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <!-- 该配置用来加载数据源,设置sessionFactory,作为spring配合文件存在,是最底层,最基础的配置 。 总览hibernate等其他的配置。 --> <beans> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"></property> <property name="username" value="zq"/> <property name="password" value="zq123"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hiberante.format_sql">true</prop> </props> </property> <property name="configLocations"> <list> <value>classpath:comm/buaa/hibernate/user.cfg.xml</value> </list> </property> </bean> <!-- 代理事务处理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 事务基类,加载bean --> <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modi*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="get*">PROPAGATION_NEVER</prop> </props> </property> </bean> </beans>
spring-core.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <import resource="comm/buaa/xml/spring-user.xml"/> </beans>
spring-user.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <bean id="uersDao" class="comm.buaa.dao.impl.UserDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 为service层加载事务 --> <bean id="userManagerBase" class="comm.buaa.service.impl.UserService"> <property name="uersDao" ref="uersDao"/> </bean> <!-- parent继承spring-hibernate.xml的事务id, id="userService" 指的是comm.buaa.control.UserControl的属性 --> <bean id="userService" parent="transactionBese"> <property name="target" ref="userManagerBase"/> </bean> </beans>
user.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping class="comm.buaa.bean.Users"/> </session-factory> </hibernate-configuration>
UserService.java
package comm.buaa.service.impl; import comm.buaa.bean.Users; import comm.buaa.dao.IUserDao; import comm.buaa.service.IUserService; public class UserService implements IUserService{ private IUserDao usersDao; public void setUsersDao(IUserDao usersDao) { this.usersDao = usersDao; } public IUserDao getUsersDao(){ return usersDao; } public void addUser(Users user) { usersDao.addUser(user); } }
解决方案
usersDAO 写错了,配置文件里是userDAO
解决方案二:
向各位大神请教个问题
解决方案三:
用annocation自動注入,方便很多,不容易出錯
时间: 2024-12-12 12:32:43