Spring XML配置到JavaConfig:继承和bean依赖

时间:2016-01-21 05:29:28

标签: spring spring-data spring-data-jpa spring-java-config

我有一个扩展AbstractService类的服务列表。 AbstractService包含DAO(您可以将其称为“存储库”)并具有DAO的getter / setter:

/**
 * @param <T> Entity type
 * @param <K> Entity ID type
 * @param <S> DAO type
 */
public abstract class AbstractService<T, K extends Serializable, S extends BaseDAO<T, K>> implements BaseService<T, K> {
    private S dao;

    public S getDAO() { return dao; }

    public void setDAO(S dao) { this.dao = dao; }

    // Then common methods to all my services, using the DAO, for instance

    @Override
    public Optional<T> findOne(K key) throws DataException {
        return Optional.ofNullable(dao.findOne(key));
    }
}

服务示例:

@Service
public class EmployeeServiceImpl extends AbstractService<Employee, Integer, EmployeeDAO> implements EmployeeService {
    // Some specific methods to that service
}

相关的DAO(我使用Spring Data JPA):

public interface EmployeeDAO extends BaseDAO<Employee, Integer> {
}

扩展

@NoRepositoryBean
public interface BaseDAO<T, K extends Serializable> extends JpaRepository<T, K> {
}

顺便提一下,我在迁移到JavaConfig时添加了注释@Service@NoRepositoryBean
我的旧XML配置是:

<bean id="com.xxx.service._AbstractService" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="com.xxx.dao._TxManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED,-com.xxx.DataException</prop>
            <prop key="update*">PROPAGATION_REQUIRED,-com.xxx.DataException</prop>
            <prop key="delete*">PROPAGATION_REQUIRED,-com.xxx.DataException</prop>
        </props>
    </property>
</bean>

<bean id="com.xxx.service.EmployeeService" parent="com.xxx.service._AbstractBO">
    <property name="target">
        <bean class="com.xxx.service.EmployeeServiceImpl">
            <property name="DAO" ref="com.xxx.dao.EmployeeDAO"/>
        </bean>
    </property>
</bean>

第一个问题,使用JavaConfig注入通用DAO和处理服务继承的正确方法是什么?
第二个问题,如何将关于事务的XML片段(com.xxx.service._AbstractBO)翻译成JavaConfig?

这是我到目前为止的两个课程:

@Configuration
@ComponentScan("com.xxx.service")
public class SpringConfig {
}

持久性配置

@Configuration
@EnableJpaRepositories("com.xxx.repository")
@EnableTransactionManagement
public class PersistenceConfig {
    /* Here so far I defined the DataSource, the EntityManagerFactory,
       the PlatformTransactionManager and the JpaVendorAdapter */
}

提前感谢您的时间!

1 个答案:

答案 0 :(得分:0)

以下是我最终要做的事情:我没有尝试翻译旧的XML配置,而是更改了类设计。因为无论如何都需要DAO,我将它们注入每个具体的类构造函数中,该构造函数调用我添加的新的抽象类构造函数。

抽象服务:

final private S dao;

public AbstractService(S dao) {
    super();
    this.dao = dao;
}

// getter protected and setter removed
protected S getDAO() {
    return dao;
}

一个具体的服务示例:

@Service
public class EmployeeServiceImpl extends AbstractService<Employee, Integer, EmployeeDAO> implements EmployeeService {

    @Inject
    public EmployeeServiceImpl(EmployeeDAO dao) {
        super(dao);
    }
}

好消息是我没有必要更改我在问题中发布的Java配置,这意味着@EnableJpaRepositories("com.xxx.repository")@ComponentScan("com.xxx.service")足以生成和绑定bean。