gpt4 book ai didi

spring - 无法 Autowiring 字段

转载 作者:行者123 更新时间:2023-12-02 07:16:16 26 4
gpt4 key购买 nike

我正在使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com