无法自动装配字段

3

我正在使用 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 和接口的包。

我的区域接口 dao.IRegion

public interface IRegion<T extends Serializable> {

    List<T> findAll();

}

我的区域数据访问对象 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
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>

你能分享一下你的Spring配置吗? - Shinichi Kai
我更新了我的问题并添加了它。 - Hayi
regionI 的 Bean 入口在哪里? - Amogh
1个回答

4

类型为RegionDao的bean从未被Spring IOC容器创建,因此该bean不受Spring管理,因此无法进行自动装配。Spring基本上在说,我没有任何可以满足控制器中此依赖项的bean。

要使RegionDao由Spring创建和管理,请在Hibernate配置文件中组件扫描类的包。

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

这将在Spring IoC容器中创建一个类型为RegionDao的bean,并使其可用于自动装配。


是的,它在里面。看起来是这样的 <property name="packagesToScan" value="dao" /> - Hayi
3
这个配置是用于Hibernate的,它是用来扫描你的实体类的包。详情请查看:http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBean.html#setPackagesToScan%28java.lang.String...%29 这个属性不会导致你的dao在Spring IOC容器中被注册为bean。 - Kevin Bowersox
@KevinBowersox 你的意思是指在servlet xml中吗? - Amogh
是的,谢谢。我有点困惑,但我把它修改成这样 <property name="packagesToScan" value="persistence" /><context:component-scan base-package="dao"/>,现在它可以工作了。 - Hayi
@KevinBowersox 哦,好的 :). 这意味着servlet dispatcher中的context:component-scan与hibernate配置文件中的context:component-scan不同。 - Amogh
1
大多数情况下,阅读更多:https://dev59.com/_nA65IYBdhLWcg3wvxaE - Kevin Bowersox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接