0

我的课程看起来像这样: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 />

测试类

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 中。也许组件扫描扫描在错误的地方?

4

1 回答 1

2

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

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

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

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

@Autowired
private ItemDAOImpl itemDAO;

因为它不能转换为 ItemDaoImpl。这将解释您面临的异常。

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

@Autowired
private DAO<Item> itemDAO;

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

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

public interface ItemDAO extends DAO<Item>

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

@Autowired
private ItemDAO itemDAO; 
于 2016-05-22T13:23:20.507 回答