Spring XML config to JavaConfig: inheritance and bean dependency -
i have list of services extend abstractservice class. abstractservice holds dao (you can call "repository") , has getter/setter dao:
/** * @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; } // common methods services, using dao, 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 { // specific methods service }
the related dao (i use spring data jpa):
public interface employeedao extends basedao<employee, integer> { }
extending
@norepositorybean public interface basedao<t, k extends serializable> extends jparepository<t, k> { }
by way added annotations @service
, @norepositorybean
while moving javaconfig.
old xml config was:
<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, proper way inject generic dao , handle service inheritance using javaconfig?
second question, how translate xml snippet transactions (com.xxx.service._abstractbo) javaconfig?
here have far, 2 classes:
@configuration @componentscan("com.xxx.service") public class springconfig { }
and persistence config
@configuration @enablejparepositories("com.xxx.repository") @enabletransactionmanagement public class persistenceconfig { /* here far defined datasource, entitymanagerfactory, platformtransactionmanager , jpavendoradapter */ }
thanks in advance time!
here ended doing: instead of trying translate old xml config, changed classes design. since daos required anyway, inject them in each concrete class constructor calls new abstract class constructor added.
the abstract service:
final private s dao; public abstractservice(s dao) { super(); this.dao = dao; } // getter protected , setter removed protected s getdao() { return dao; }
and concrete service example:
@service public class employeeserviceimpl extends abstractservice<employee, integer, employeedao> implements employeeservice { @inject public employeeserviceimpl(employeedao dao) { super(dao); } }
the thing didn't have change java config posted in question, meaning @enablejparepositories("com.xxx.repository")
, @componentscan("com.xxx.service")
sufficient generate , bind beans.
Comments
Post a Comment