Spring bean构造函数arg与泛型

时间:2011-11-11 09:17:01

标签: java spring google-app-engine spring-mvc autowired

我会让我的代码为我说话,首先,这是我的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);
    }
}

现在我的日志是:

  

AVVERTENZA:嵌套于   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'usersController'的bean在文件中定义时出错   [/home/fabio/stsworkspace/TestGAE/target/TestGAE-1.0-SNAPSHOT/WEB-INF/classes/it/trew/prove/web/UsersController.class]:通过构造函数参数表达的不满意的依赖关系   类型为[it.trew.prove.server.services.UsersService]的索引0 ::错误   在ServletContext中定义名为'usersService'的bean   资源[/WEB-INF/spring/root-context.xml]:不满意的依赖项   通过类型为索引0的构造函数参数表示   [it.trew.prove.model.dao.UsersDao] ::创建名称为bean的错误   'usersDao'在ServletContext资源中定义   [/WEB-INF/spring/root-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资源中定义名为'usersDao'的bean   [/WEB-INF/spring/root-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.UnsatisfiedDependencyException:   在ServletContext中定义名为'usersService'的bean时出错   资源[/WEB-INF/spring/root-context.xml]:不满意的依赖项   通过类型为索引0的构造函数参数表示   [it.trew.prove.model.dao.UsersDao] ::创建名称为bean的错误   'usersDao'在ServletContext资源中定义   [/WEB-INF/spring/root-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资源中定义名为'usersDao'的bean   [/WEB-INF/spring/root-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():   java.lang.NoSuchMethodException:   it.trew.prove.model.dao.UsersDao。()

所以...在您的诚实意见中,如何更改我的代码以使其正常工作?

  • 很抱歉这么详细:) -

3 个答案:

答案 0 :(得分:2)

将构造函数参数添加到配置文件

<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"/>

答案 1 :(得分:1)

  

找不到默认构造函数;嵌套异常是java.security.PrivilegedActionException:java.lang.NoSuchMethodException:

只需添加默认构造函数

答案 2 :(得分:0)

要使@Autowire注释生效,您必须添加<context:annotation-config />。例如,

<?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>
相关问题