1、报错信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcTemplate' of bean class [com.sukoyakaken.dao.impl.UserDaoImpl]: Bean property 'jdbcTemplate' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1642)
...
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcTemplate' of bean class [com.sukoyakaken.dao.impl.UserDaoImpl]: Bean property 'jdbcTemplate' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:243)
...
2、applicationContext
<!--加载jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置UserDao-->
<bean id="userDao" class="com.sukoyakaken.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--配置UserService-->
<bean id="userService" class="com.sukoyakaken.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--配置RoleDao-->
<bean id="roleDao" class="com.sukoyakaken.dao.impl.RoleDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--配置RoleService-->
<bean id="roleService" class="com.sukoyakaken.service.impl.RoleServiceImpl">
<property name="roleDao" ref="roleDao"/>
</bean>
3、controller
@RequestMapping("/user")
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public ModelAndView list() {
List<User> userList = userService.list();
ModelAndView modelAndView = new ModelAndView();
// 设置模型
modelAndView.addObject("userList", userList);
// 设置视图
modelAndView.setViewName("user-list");
return modelAndView;
}
}
4、service
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<User> list() {
List<User> userList = userDao.findAll();
return userList;
}
}
5、dao
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<User> findAll() {
List<User> userList = jdbcTemplate.query("select * from sys_user", new BeanPropertyRowMapper<User>(User.class));
return userList;
}
}
6、补充:
我还有一个RoleController、RoleService、RoleDao,也是按这一套流程写的,就名字和页面不同,但它就可以成功运行,所以应该可以确定环境之类的没有问题。只要加上上面User的代码,tomcat就会报错。求家人们解救!感谢!(T人T)
UserDaoImpl里面加上
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
其他的dao也同理
代码压缩丢网盘,这个错误有点意思
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcTemplate' of bean class [com.sukoyakaken.dao.impl.UserDaoImpl]: Bean property 'jdbcTemplate' is not writable or has an invalid setter method.
错误不都写出来了,问题解决了嘛