component-scan没有找到@Repositoy

时间:2016-05-22 12:35:45

标签: java spring component-scan

我的课程如下: AbstractDao的

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 />

TEST课程

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());
}
}

此代码不应该能够自动装配我的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中。也许组件扫描扫描在错误的地方?

1 个答案:

答案 0 :(得分:2)

因为ItemDAOImpl是用@Transactional注释的,所以spring会为这个bean创建一个代理,并在自动装配时注入代理,而不是bean本身。

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

我遇到了类似你描述的问题,原因是,那个春天使用了Jdk代理而且我没有意识到它。

在你的情况下,ItemDaoImpl的spring bean将是一个实现DAO的代理。 这不能注入

@Autowired
private ItemDAOImpl itemDAO;

因为它无法强制转换为ItemDaoImpl。 这可以解释你所面临的例外情况。

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

@Autowired
private DAO<Item> itemDAO;

如果您使用的是弹簧4,则上述方法仅适用。

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

public interface ItemDAO extends DAO<Item>

让ItemDaoImpl实现它。 最后更改您希望将其注入

的字段
@Autowired
private ItemDAO itemDAO; 
相关问题