org.springframework.beans.factory.NoSuchBeanDefinitionException:没有限定bean

时间:2017-12-28 18:48:39

标签: java spring hibernate spring-mvc

我正在使用Spring-mvc创建一个ShoppingCart应用程序。在dao层我正在使用Spring-Hibernate ORM功能。

@Repository("baseDao")
public class BaseDaoImpl<T extends ShoppingCartEntity> implements BaseDao<T>
 {
   @Autowired
   protected SessionFactory sessionFactory;

   private Class<T> entityClass;

   public BaseDaoImpl(Class<T> entityClass) {
      super();
      this.entityClass = entityClass;
   }

   public Serializable save(T entity) {
      return sessionFactory.getCurrentSession().save(entity); 
   }


   public void update(T entity) {
      sessionFactory.getCurrentSession().update(entity); 
   }


   public void saveOrUpdate(T entity) {
       sessionFactory.getCurrentSession().saveOrUpdate(entity); 
   }
 }

CartDaoImpl.java

@Repository("cartDao")
public class CartDaoImpl extends BaseDaoImpl<CartMaster> implements CartDao {

   private Class<CartMaster> entityClass;

   public CartDaoImpl(@Value("com.orgn.shc.entity.CartMaster")Class<CartMaster> entityClass) {
       super(entityClass);
       this.entityClass = entityClass;
    }
 //other getter & setter
  }

我有一个名为ShoppingCartEntity.java的基本实体类。

@MappedSuperclass
public class ShoppingCartEntity implements Serializable {

  private static final long serialVersionUID = 1L;
   //other fields
}

CartMaster.java:

   @Entity
   @Table(name = "SHOP_CART_MASTER")
   public class CartMaster extends ShoppingCartEntity {
  //other fields
   }

现在,在weblogic中部署应用程序期间,我遇到以下异常:

   <28 Dec, 2017, 11:48:08,916 PM IST> <Error>  
   <org.springframework.web.servlet.DispatcherServlet> <BEA-000000> <Context   
    initialization failed
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error   
    creating bean with name 'baseDao' defined in URL [zip:C:/Oracle 
    /Middleware/ORACLE_HOME/user_projects/domains/wl_server/servers 
    /AdminServer/tmp/_WL_user/shoppingcart/8i2b70/war/WEB-INF/lib 
    /wl_cls_gen.jar!/com/orgn/shc/dao/impl/BaseDaoImpl.class]: Unsatisfied 
    dependency expressed through constructor parameter 0; nested exception is  
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
    qualifying bean of type 'java.lang.Class<?>' available: expected at least 
    1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at   
 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)

它的spring-bean配置部分如下

   <jee:jndi-lookup id="dataSource" jndi-name="jdbc/SHPDS"
    expected-type="javax.sql.DataSource" />

   <bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="com.orgn.shc.entity" />
      <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
        </props>
      </property>
   </bean>

  <mvc:annotation-driven />
  <context:component-scan base-package="com.orgn.shc" />


  <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

 <mvc:resources mapping="/assets/**" location="/assets/" />

我认为构造函数级别的 @Value annotation 足以在子类中相应地注入依赖项。

任何人都可以有任何合适的解决方案吗?

1 个答案:

答案 0 :(得分:0)

@Repository("baseDao")中删除BaseDaoImpl,因为它不应该作为单独的bean创建;相反,它应该仅用作其他DAO类的父级。我也会使它abstract及其构造函数protected以避免意外实例化。

此外,您似乎需要将CartDaoImpl的构造函数更改为以下内容:

public CartDaoImpl() {
   super(CartMaster.class);
}

我不相信你在entityClass中确实需要CartDaoImpl字段,因为你知道这个DAO中你的实体的类:它是CartMaster.class。同时,在BaseDaoImpl中,entityClass字段可能对从数据库中读取实体很有用(即使您没有这样的方法)。