我将让我的代码为我说话,首先,下面是我的root-context.xml:
<context:component-scan base-package="it.trew.prove" />
<bean id="usersDao" class="it.trew.prove.model.dao.UsersDao" />
<bean id="usersService" class="it.trew.prove.server.services.UsersServiceImpl" />我的一些用户道:
public class UsersDao extends ObjectifyDao<User> {
protected UsersDao(Class<User> clazz) {
super(User.class);
}
static {
ObjectifyService.register(User.class);
}
}以及我的用户服务(实现):
public class UsersServiceImpl implements UsersService {
private final UsersDao usersDao;
@Autowired
public UsersServiceImpl(UsersDao usersDao) {
this.usersDao = usersDao;
}
@Override
public List<User> listUsers() {
return usersDao.list();
}
@Override
public void saveUser(User user) {
usersDao.add(user);
}
}现在我的日志是:
org.springframework.beans.factory.UnsatisfiedDependencyException:中嵌套的/home/fabio/stsworkspace/TestGAE/target/TestGAE-1.0-SNAPSHOT/WEB-INF/classes/it/trew/prove/web/UsersController.class:错误创建名为“usersController”的bean,通过构造函数参数(索引为0的it.trew.prove.server.services.UsersService::创建ServletContext资源/WEB/spring/root-context.xml中定义的usersService名称的bean )表示不满意的依赖关系:通过构造函数参数(索引为it.trew.prove.model.dao.UsersDao::usersDao)创建ServletContext资源/WEB/ServletContext/root-context.xml中定义的名称为'usersDao‘的bean时表示的不满意依赖:ServletContext失败的实例化;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类it.trew.prove.model.dao.UsersDao:未找到默认构造函数;嵌套异常为java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao。();嵌套异常是org.springframework.beans.factory.BeanCreationException:错误,在ServletContext资源/WEB/spring/root-context.xml中定义了名为“usersDao”的bean : bean失败的实例化;嵌套异常是org.springframework.beans.BeanInstantiationException:不能实例化bean类it.trew.prove.model.dao.UsersDao:没有找到默认构造函数;嵌套异常是java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao.();嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:错误创建bean的名称'usersService‘定义在ServletContext资源/WEB/spring/根-context.xml:不满意的依赖表达通过构造函数参数的类型为it.trew.prove.model.dao.UsersDao::错误创建名称'usersDao’在ServletContext资源/WEB/spring/rot-context.xml:实例化bean失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类it.trew.prove.model.dao.UsersDao:未找到默认构造函数;嵌套异常为java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao。();嵌套异常是org.springframework.beans.factory.BeanCreationException:错误,在ServletContext资源/WEB/spring/root-context.xml中定义了名为“usersDao”的bean : bean失败的实例化;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类it.trew.prove.model.dao.UsersDao:没有找到默认构造函数;嵌套异常是java.security.PrivilegedActionException: java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao.():java.lang.NoSuchMethodException: it.trew.prove.model.dao.UsersDao.()
所以..。老实说,如何更改我的代码以使其正确工作?
发布于 2011-11-11 09:23:29
将构造函数参数添加到配置文件中
<bean id="usersDao" class="it.trew.prove.model.dao.UsersDao">
<constructor value="it.trew.prove.model.dao.User" />
</bean>或者更好的是,从UserDao构造函数中删除无用的参数!
并为用户服务bean声明添加autowire="constructor"。<bean id="usersService" class="it.trew.prove.server.services.UsersServiceImpl" autowire="constructor"/>
发布于 2011-11-11 09:18:24
未找到默认构造函数;嵌套异常为java.security.PrivilegedActionException: java.lang.NoSuchMethodException:
只需添加默认构造函数即可
发布于 2011-11-11 09:21:41
要使@Autowire注释正常工作,必须添加 .E.g。
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>https://stackoverflow.com/questions/8091831
复制相似问题