无法自动装配字段 [英] Could not autowire field

查看:198
本文介绍了无法自动装配字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Hibernate 4 + Spring MVC 4 ,当我开始 Apache Tomcat服务器8 出现此错误:

I'm using Hibernate 4 + Spring MVC 4 and when i start Apache Tomcat Server 8 I got this error :

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 =packagesToScanvalue =dao/>

here's my Hibernate Configuration which contain <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 is the package where i put my dao and interfaces.

我的地区界面 dao.IRegion

My region 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
public class welcome {

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

}

我的servlet调度程序

My servlet dispatcher

<?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管理的,这使得它不可用于自动装配。 Spring基本上是说,我没有任何bean在控制器中满足这个依赖。

A bean of type RegionDao is never created by the Spring IOC Container, therefore the bean is not managed by Spring, which makes it unavailable for autowiring. Spring is basically saying, I do not have any bean that satisfies this dependency in the controller.

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

To make RegionDao be created and managed by Spring, component scan the class's package in the hibernate configuration file.

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

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

This will create a bean of type RegionDao inside the Spring IOC Container and make it available for autowiring.

这篇关于无法自动装配字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆