Spring AutoWired Exception

时间:2011-11-29 10:49:44

标签: spring autowired

这是错误......

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.

3 个答案:

答案 0 :(得分:3)

关键是:

找不到类型为[com.iu.eblood.daoImp.CategoryDaoImp]的匹配bean依赖:预计至少有一个bean可以作为此依赖项的autowire候选者

这意味着你需要至少拥有在XML或@Component

中定义的bean

答案 1 :(得分: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>

答案 2 :(得分:0)

你的spring XMLs中有这一行吗?

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

如果有,你试着用

注释你的Dao
@Repository

而不是

@Repository("categorydaoimp")