简体   繁体   English

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

[英]Spring XML config to JavaConfig: inheritance and bean dependency

I have a list of services that extend an AbstractService class. 我有扩展AbstractService类的服务列表。 The AbstractService holds the DAO (you can call it "repository") and has the getter/setter for the DAO: 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));
    }
}

A service example: 服务示例:

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

The related DAO (I use Spring Data JPA): 相关的DAO(我使用Spring Data JPA):

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

Extending 延伸

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

By the way I added the annotations @Service and @NoRepositoryBean while moving to JavaConfig. 顺便说一下,我在移至JavaConfig时添加了注释@Service@NoRepositoryBean
My old XML config was: 我以前的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>

First question, what is the proper way to inject the generic DAO and handle the service inheritance using JavaConfig? 第一个问题,使用JavaConfig注入通用DAO和处理服务继承的正确方法是什么?
Second question, how to translate the XML snippet about transactions (com.xxx.service._AbstractBO) to JavaConfig? 第二个问题,如何将有关事务的XML代码段(com.xxx.service._AbstractBO)转换为JavaConfig?

Here is what I have so far, 2 classes: 到目前为止,这里有2个课程:

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

And the persistence config 和持久性配置

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

Thanks in advance for your time! 在此先感谢您的时间!

Here is what I ended up doing: instead of trying to translate my old XML config, I changed the classes design. 这就是我最终要做的事情:我没有尝试转换旧的XML配置,而是更改了类设计。 Since DAOs are required anyway, I inject them in each concrete class constructor which calls a new abstract class constructor I added. 由于仍然需要DAO,因此我将它们注入每个具体的类构造函数中,该构造函数调用我添加的新抽象类构造函数。

The abstract service: 抽象服务:

final private S dao;

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

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

And a concrete service example: 还有一个具体的服务示例:

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

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

The good thing is I didn't have to change the Java config I posted in my question, meaning @EnableJpaRepositories("com.xxx.repository") and @ComponentScan("com.xxx.service") are sufficient to generate and bind beans. 好处是我不必更改我在问题中发布的Java配置,这意味着@EnableJpaRepositories("com.xxx.repository")@ComponentScan("com.xxx.service")足以生成和绑定豆子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM