2011-11-29 135 views
0

以下是錯誤...彈簧自動裝配異常

 
Deployment Error for module: EBlood: Error occurred during deployment: 
Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'categoryService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: public com.iu.eblood.daoImp.CategoryDaoImp com.iu.eblood.serviceImp.CategoryService.categoryDaoImp; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No matching bean of type [com.iu.eblood.daoImp.CategoryDaoImp] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=categorydaoimp)}. 
Please see server.log for more details. 
+0

註釋你的道向我們展示你的代碼 – skaffman

回答

3

的關鍵是這樣的:

類型[com.iu.eblood.daoImp.CategoryDaoImp]沒有匹配的bean找到相關性:預期至少1個Bean上有資格作爲自動裝配候選人,這種依賴性

,這意味着你需要有至少上無論是在XML或@Component

0
package com.iu.eblood.dao; 

import java.io.Serializable; 
import java.util.Collection; 

public interface GenericDaoInterface<T,PK extends Serializable> { 

/** Persist the newInstance object into database */ 
PK create(T newInstance); 

/** Retrieve an object that was previously persisted to the database using 
* the indicated id as primary key 
*/ 
T read(PK id); 

/** Save changes made to a persistent object. */ 
void update(T transientObject); 

/** Remove an object from persistent storage in the database */ 
void delete(T persistentObject); 


/*Get all Data giving specific**/ 
Collection<T> loadAll(); 

} 


package com.iu.eblood.dao; 

import java.io.Serializable; 

import java.util.Collection; 


    import org.hibernate.criterion.Restrictions; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.orm.hibernate3.HibernateTemplate; 




    public abstract class GenericDao<T,PK extends Serializable> 
    implements GenericDaoInterface<T, Serializable> 
    { 
@Autowired 
protected HibernateTemplate hibernateTemplate; 


public GenericDao(Class<T> persistentClass) 
{ 
    setPersistentClass(persistentClass);  
} 

private Class<T> persistentClass; 

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

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

public Serializable create(T newInstance) { 
    return hibernateTemplate.save(newInstance); 

} 

public T read(Serializable id) { 
    return (T) hibernateTemplate.get(persistentClass,id); 
} 

public void update(T transientObject) { 

    hibernateTemplate.update(transientObject); 

} 

public void delete(T persistentObject) { 

    /* 
    * Update DeletedDate for Entity*/ 
    update(persistentObject); 

} 

public Collection<T> loadAll() { 
    return hibernateTemplate.loadAll(persistentClass); 

} 

@SuppressWarnings("unchecked") 
public T loadByField(String fieldName, Object fieldValue) { 

    return (T) hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass). 
      add(Restrictions.eq(fieldName, fieldValue)).uniqueResult(); 
} 

@SuppressWarnings("unchecked") 
public Collection<T> loadAllByField(String fieldName, Object fieldValue) { 

    return  hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass). 
      add(Restrictions.eq(fieldName, fieldValue)).list(); 
} 

} 


package com.iu.eblood.daoImp; 


import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.orm.hibernate3.HibernateTemplate; 
import org.springframework.stereotype.Repository; 

    import com.iu.eblood.dao.GenericDao; 
    import com.iu.eblood.model.Category; 

    @Repository("categorydaoimp") 
    public class CategoryDaoImp extends GenericDao<Category, Long> { 


public CategoryDaoImp() { 

    super(Category.class); 
} 


} 
定義豆

Appcontext內容

<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="DB2DataSource" /> 
    <property name="packagesToScan" value="com.iu.eblood.model" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.format_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
     </props> 
    </property> 
</bean> 

<bean id="hibernateTemplate"  class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <constructor-arg> 
     <ref bean="sessionFactory" /> 
    </constructor-arg> 
</bean> 
0

你有你的春天個XML這一行?

<context:component-scan base-package="com.iu.eblood"/> 

如果是的話你有沒有試着用

@Repository 

,而不是

@Repository("categorydaoimp")