spring - 无法 Autowiring 字段

标签 spring hibernate spring-mvc

我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误:

Error creating bean with name 'welcome': Injection of autowired dependencies failed;
Could not autowire field: private dao.IRegion controller.welcome.regionI;
No qualifying bean of type [dao.IRegion] 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)}

这是我的 Hibernate 配置,其中包含 <property name="packagesToScan" value="dao" /> :

<?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.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <context:property-placeholder location="persistence-mysql.properties" />

   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="dao" />
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
         </props>
      </property>
   </bean>

<!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
   <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.user}" />
      <property name="password" value="${jdbc.pass}" />
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

dao 是我放置 dao 和接口(interface)的包。

我的区域界面dao.IRegion :

public interface IRegion<T extends Serializable> {

    List<T> findAll();

}

我的区域 DAO dao.RegionDAO

@Repository
public class RegionDAO  implements IRegion < Region > {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Region> findAll() {

        return  sessionFactory.getCurrentSession().createQuery("from Region").list();
    }

}

我的 Controller

@Controller
public class welcome {

    @Autowired
    private IRegion<Region> regionI;
        ....

}

我的 servlet 调度程序

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:default-servlet-handler />
    <context:component-scan base-package="controller"/>
    <mvc:annotation-driven />

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

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


</beans>

最佳答案

RegionDao 类型的 bean 永远不会由 Spring IOC 容器创建,因此该 bean 不受 Spring 管理,这使得它无法进行 Autowiring 。 Spring 基本上是说,我在 Controller 中没有任何满足此依赖关系的 bean。

为了让 RegionDao 由 Spring 创建和管理,组件会扫描 hibernate 配置文件中类的包。

<context:component-scan base-package="package.with.daos"/>

这将在 Spring IOC 容器内创建一个 RegionDao 类型的 bean,并使其可用于 Autowiring 。

关于spring - 无法 Autowiring 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24469109/

相关文章:

spring - 使用Spring Yarn时如何向容器分配资源?

java - spring读取属性文件时出现404错误

java - DispatcherServletRegistration Spring启动异常

java - 使用 spring mvc 和 hibernate 在 jsp 上显示图像

java - 在 Dao 方法中使用参数来获取实体是只读访问的最有效方法

java - 需要配置Spring Security和Hibernate

java - Hibernate/JPA 错误 - 无法识别枚举中的某些字符串

hibernate - 使用 hibernate + spring mvc 获取自动生成的 key ,保存时

mysql - Hibernate ObjectNotFoundException,即使表中存在记录

java - IntelliJ IDEA 13.1.1 - Spring MVC 项目模板错误