spring 无法自动装配字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24469109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not autowire field
提问by Youssef
I'm using Hibernate 4
+ Spring MVC 4
and when i start Apache Tomcat Server 8
I got this error :
我正在使用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)}
here's my Hibernate Configuration which contain <property name="packagesToScan" value="dao" />
:
这是我的休眠配置,其中包含<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>
daois the package where i put my dao and interfaces.
dao是我放置 dao 和接口的包。
My region Interface dao.IRegion
:
我的地区界面dao.IRegion
:
public interface IRegion<T extends Serializable> {
List<T> findAll();
}
my Region DAO dao.RegionDAO
我的区域 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();
}
}
My Controller
我的控制器
@Controller
public class welcome {
@Autowired
private IRegion<Region> regionI;
....
}
My servlet dispatcher
我的 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>
回答by Kevin Bowersox
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 IOC Container 永远不会创建类型的 bean,因此 bean 不受 Spring 管理,这使得它不能用于自动装配。Spring 基本上是在说,我在控制器中没有任何满足这种依赖关系的 bean。
To make RegionDao
be created and managed by Spring, component scan the class's package in the hibernate configuration file.
为了让RegionDao
Spring创建和管理,组件扫描hibernate配置文件中类的包。
<context:component-scan base-package="package.with.daos"/>
This will create a bean of type RegionDao
inside the Spring IOC Container and make it available for autowiring.
这将RegionDao
在 Spring IOC Container 中创建一个类型为 bean 的 bean并使其可用于自动装配。