java - 组件扫描未找到@Repository

标签 java spring component-scan

我的类(class)如下所示: 抽象DAO

package dao.impl;
public abstract class AbstractDAO <E> implements DAO<E> {

@PersistenceContext
private EntityManager em;

   public void add(E entity) {
        em.persist(entity);
   }
}

DAOImpl

package dao.impl;
@Transactional
@Repository
public class ItemDAOImpl extends AbstractDAO<Item> {

}

应用程序上下文-test.xml

<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:component-scan base-package="dao.impl" />

<bean id="entityManagerFactoryBean"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="packagesToScan" value="domain" />

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.archive.autodetection">class</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/inventory" />
    <property name="username" value="root" />
    <property name="password" value="1234" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>

<tx:annotation-driven />

测试类

package service.impl;

@ContextConfiguration(locations = "classpath:application-context-test.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ItemTest {

@Autowired
private ItemDAOImpl itemDAO;

@Test
public void testCreateItem() throws Exception {
    Item item = new Item("cellphone", "galaxy", ItemType.TECHNOLOGY, 10000);
    itemDAO.add(item);        
    assertEquals(5, itemDAO.list(Item.class).size());
}
}

这段代码不应该能够 Autowiring 我的 itemDAO 吗?

当我运行测试时,它会抛出异常

org.springframework.beans.factory.BeanCreationException: Could not autowire 
field: private dao.impl.ItemDAOImpl service.impl.ItemTest.itemDAO; nested
exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [dao.impl.ItemDAOImpl] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations: 

你能告诉我我错过了什么吗?我唯一能想到的是,由于我的测试位于 src/test/java 中,所以我的 application-context-test.xml 位于 src/test/resources 中,而我的 dao 位于 src/main/java 中。也许组件扫描扫描在错误的地方?

最佳答案

因为 ItemDAOImpl 使用 @Transactional 注解,所以 spring 会为这个 bean 创建一个代理,并在 Autowiring 时注入(inject)该代理,而不是 bean 本身。

Spring 可以通过子类化(使用 Cglib)或通过使用 Jdk 代理实现 beans 接口(interface)来创建代理。 spring 使用哪种类型的代理,取决于您的配置。

我遇到了和你描述的类似的问题,原因是spring使用了Jdk代理,而我没有意识到这一点。

在您的情况下,ItemDaoImpl 的 spring bean 将是实现 DAO 的代理。 这无法注入(inject)

@Autowired
private ItemDAOImpl itemDAO;

因为它无法转换为 ItemDaoImpl。 这可以解释您所面临的异常。

要解决此问题,请将字段更改为

@Autowired
private DAO<Item> itemDAO;

以上仅适用于您使用 spring 4 的情况。

使用早期版本的 spring,您必须创建一个接口(interface)

public interface ItemDAO extends DAO<Item>

并让 ItemDaoImpl 实现它。 最后更改您想要将其注入(inject)的字段

@Autowired
private ItemDAO itemDAO; 

关于java - 组件扫描未找到@Repository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37374449/

相关文章:

java - Java中的一种负载平衡线程池

javascript - 在包含 JsonIdentityInfo 的 JavaScript 中反序列化 Jackson 对象

java - Spring Boot 在外部 tomcat 部署时抛出 NoSuchMethodError org.springframework.util.StringUtils.isEmpty

java - 如何使用 Spring Boot 避免由另一个库配置外部库

java - 读取由java中的空格分隔的输入

java - 无法将 void 转换为 "classname"

spring - 获取错误-java.lang.NoClassDefFoundError : javax/faces/component/behavior/ClientBehaviorHolder

java - 为什么我的配置类没有用 @Configurable 注释,并且 @ComponentScan 没有初始化 beans 两次?

java - 上下文中的多个包:component-scan, spring config

java - NPE on java stream reduce操作