Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过索引为[java.lang.Class]类型的0的构造函数参数表示的未满足的依赖关系_Java_Spring - Fatal编程技术网

通过索引为[java.lang.Class]类型的0的构造函数参数表示的未满足的依赖关系

通过索引为[java.lang.Class]类型的0的构造函数参数表示的未满足的依赖关系,java,spring,Java,Spring,我使用hibernate和spring创建了一个简单的web应用程序,我想实现一个包含crud操作的抽象类,但我有以下错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientService' defined in class path resource [applicationContext.xml]: Cannot resolve referen

我使用hibernate和spring创建了一个简单的web应用程序,我想实现一个包含crud操作的抽象类,但我有以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientService' defined in class path resource [applicationContext.xml]: 
Cannot resolve reference to bean 'clientDao' while setting bean property 'clientDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'clientDao' defined in class path resource [applicationContext.xml]: 
Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class]: 
通用刀

public interface GenericDao<T, ID extends Serializable> {
    
    T save(T entity);
    T update(T entity);
    void delete(T entity);
    T findById(ID id);
    List<T> findAll();
    void flush();
公共接口GenericDao{
T保存(T实体);
T更新(T实体);
无效删除(T实体);
T findById(ID);
列出findAll();
无效冲洗();
}

通用DAOImpl

@Transactional
public  class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID> {
    
    @Autowired
    SessionFactory sessionFactory ;
    
 private Class<T> persistentClass;
     
     
    
    public GenericDaoImpl() {
    super();
}

    public GenericDaoImpl(Class<T> persistentClass) {
    super();
    this.persistentClass = persistentClass;
}

    @Transactional
    public T save(T entity) {
        this.sessionFactory.getCurrentSession().save(entity);
        return null;
    }

    @Transactional
    public T update(T entity) {
        this.sessionFactory.getCurrentSession().update(entity);
        return null;
    }

    @Transactional
    public void delete(T entity) {
        this.sessionFactory.getCurrentSession().delete(entity);
        
    }

    @SuppressWarnings("unchecked")
    @Transactional
    public T findById(ID id) {
        return  (T) this.sessionFactory.getCurrentSession().load(this.getPersistentClass(), id);
        
    }
    @SuppressWarnings("unchecked")
    @Transactional
    public List<T> findAll() {
        return   this.sessionFactory.getCurrentSession().createQuery("* from"+this.getPersistentClass().getSimpleName()).list();
    }

    @Transactional
    public void flush() {
        this.sessionFactory.getCurrentSession().flush();
        
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    public void setPersistentClass(Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }
    
    

}
@Transactional
公共类GenericDaoImpl实现GenericDao{
@自动连线
会话工厂会话工厂;
私有类persistentClass;
公共通用DAOImpl(){
超级();
}
公共GenericDaoImpl(类persistentClass){
超级();
this.persistentClass=persistentClass;
}
@交易的
公共T保存(T实体){
this.sessionFactory.getCurrentSession().save(实体);
返回null;
}
@交易的
公共T更新(T实体){
this.sessionFactory.getCurrentSession().update(实体);
返回null;
}
@交易的
公共作废删除(T实体){
this.sessionFactory.getCurrentSession().delete(实体);
}
@抑制警告(“未选中”)
@交易的
公共T findById(ID){
返回(T)this.sessionFactory.getCurrentSession().load(this.getPersistentClass(),id);
}
@抑制警告(“未选中”)
@交易的
公共列表findAll(){
返回此.sessionFactory.getCurrentSession().createQuery(“*from”+此.getPersistentClass().getSimpleName()).list();
}
@交易的
公共图书馆{
this.sessionFactory.getCurrentSession().flush();
}
public SessionFactory getSessionFactory(){
返回工厂;
}
public void setSessionFactory(SessionFactory SessionFactory){
this.sessionFactory=sessionFactory;
}
公共类getPersistentClass(){
返回persistentClass;
}
public void setPersistentClass(类persistentClass){
this.persistentClass=persistentClass;
}
}
ClientDao

public interface ClientDao  extends GenericDao<Client,Integer>  {



}
公共接口ClientDao扩展了GenericDao{
}
ClientDaoImpl

@Transactional
@Repository("clientDao")
public class ClientDaoImpl extends GenericDaoImpl<Client,Integer>  implements ClientDao {
    
    
    

    
    
    public ClientDaoImpl(Class<Client> persistentClass) {
        super(persistentClass);
        
    }
@Transactional
@存储库(“clientDao”)
公共类ClientDaoImpl扩展了GenericDaoImpl实现了ClientDao{
公共ClientDaoImpl(类persistentClass){
超级(persistentClass);
}
application context.xml

<bean id="client" class="com.webapp.model.Client"/>

  <bean id="genericDao" class="com.webapp.dao.GenericDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="clientDao" class="com.webapp.dao.ClientDaoImpl" parent="genericDao">
    <constructor-arg ref="client" />
   </bean>

 <bean id="clientService" class="com.webapp.service.ClientServiceImpl">
        <property name="clientDao" ref="clientDao" />
    </bean>

ClientDaoImpl
类中定义的构造函数需要类型为
class
的参数。但是在applicationContext.xml中,您设置了要传递给构造函数的实例客户机对象

更改构造函数以接收对象并将类传递给super,例如:

public ClientDaoImpl(Client client) {
        super(client.getClass());

    }
使用:

WEB-INF/XXX-XX.xml]:通过索引为[org.springframework.security.WEB.context.SecurityContextRepository]类型的0的构造函数参数表示的未满足的依赖关系:不明确的构造函数参数类型-是否将正确的bean引用指定为构造函数参数

解决方案是从构造函数参数中删除name属性(如果有)。只保留引用。
它会起作用。

多亏了@Atul。如果我不只是通过从servlet上下文XML文件中删除name属性来修复错误消息,我永远不会想到这会起作用。换句话说,我从:

 <bean id="myBeanId" class="com.comp.Something">
   <constructor-arg name="userPreference" ref="preferencesDao" />
   <constructor-arg name="user" ref="userDao" />
 </bean>

致:


.
少即是多。另外,我收到的错误消息说,
通过构造函数参数1表示的未满足的依赖关系:类型为…
的参数的参数值不明确,而没有提到“索引”值。(这可能只是消息随着时间的推移而改变,因为这个问题已经存在六年了)。虽然仔细阅读时这一点可能很明显,数字指的是Java构造函数中的索引,而不是包含该bean的构造函数参数的上下文文件列表中的索引。虽然它们的顺序通常相同,但它们不必相同。(在代码投入生产一段时间后,上下文XML和它背后的Java代码可能会出现一些“漂移”)也就是说,我刚刚不得不更改的代码大约有4-5年的历史,而这个问题是在最近的构建中突然出现的

public ClientDaoImpl() {
    super(com.xxx.Client.class);
}
 <bean id="myBeanId" class="com.comp.Something">
   <constructor-arg name="userPreference" ref="preferencesDao" />
   <constructor-arg name="user" ref="userDao" />
 </bean>
<bean id="myBeanId" class="com.comp.Something">
  <constructor-arg ref="preferencesDao" />
  <constructor-arg ref="userDao" />
</bean>.