Spring自动装配和类继承

Chr*_*uss 1 inheritance spring autowired

我在使用@Autowired工作时遇到了问题.对不起,如果我搞砸了任何条款,我对Spring来说相对较新.

Spring Version是3.0.5.RELEASE,我在bean定义中使用context:component-scan.

这适用于@Autowired注释:

@Component
public class UserDao {
    @PersistenceContext
    protected EntityManager em;

    @Transactional
    public User findById(Long id) {
        return em.find(User.class, id);
    }
}
Run Code Online (Sandbox Code Playgroud)

这不适用于@Autowired注释:

@Component
public class UserDao implements Dao<User> {
    @PersistenceContext
    protected EntityManager em;

    @Transactional
    public User findById(Long id) {
         return em.find(User.class, id);
    }
}
Run Code Online (Sandbox Code Playgroud)

通过这个设置,我已经添加了'实现Dao',我得到了一个:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [web.rs.persistence.dao.UserDao] 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)}
Run Code Online (Sandbox Code Playgroud)

以下是一些其他类供参考:

Dao.java(界面):

public interface Dao<T extends BaseEntity> {
    T findById(Long id);
}
Run Code Online (Sandbox Code Playgroud)

UserResource.java:

@Component
public class UserResource {
    @Autowired
    UserDao userDao;

    public User getUser(Long id) {
        return userDao.findById(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

applicationContext.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"
        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.0.xsd">

        <context:component-scan base-package="web.rs" />

        <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:config.properties" />
        </bean>

        <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
            </property>
            <property name="persistenceUnitName" value="${persistence.unit}" />
        </bean>

        <bean id="trx-manager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="emf" />
        </bean>

        <tx:annotation-driven transaction-manager="trx-manager" />
    </beans>
Run Code Online (Sandbox Code Playgroud)

任何人都可以对这个问题有所了解吗?我喜欢保持阶级继承.

谢谢!

Sim*_* LG 7

使用@Transactional注释时,Spring会在类实现接口时创建JDK代理.在您的情况下,(UserDao实现Dao <User>)的JDK代理将实现Dao <User>但不会扩展UserDao.因此,上下文中的bean将是Dao <User>.

当具有@Transaction批注的类没有实现接口时,Spring必须创建一个扩展UserDao的CGLIB代理.因此,上下文中的bean将是UserDao.

在将它放入applicationContext.xml时,您可以告诉Spring始终使用CGLIB代理:

 <tx:annotation-driven transaction-manager="trx-manager" proxy-target-class="true" />
Run Code Online (Sandbox Code Playgroud)

有一些缺点,但我不记得了.

我不使用proxy-target-class ="true",我的设计是这样的:

我为每种类型的Dao都有一个界面.

 public interface UserDao extends Dao<User>

   List<User> findByUsername();
Run Code Online (Sandbox Code Playgroud)

我实现了特定的接口

 @Component
 public class UserDaoJpa implements UserDao

   public List<User> findByUsername() {
     ...
   }
Run Code Online (Sandbox Code Playgroud)

我的服务类使用UserDao:

 public class UserService {

   @Autowired
   private UserDao userDao;
 }
Run Code Online (Sandbox Code Playgroud)

上下文中的bean是UserDaoJpa,它将在使用UserDao的地方注入.