1
我想使用最新版本的spring-batch-admin-manager(v.1.3.0)將我現有的Spring批處理作業遷移到Spring-Boot web應用程序。帶有兩個JPA數據庫的春季啓動webapp
@Configuration
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class, DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class })
@Import(MainConfiguration.class) @EnableTransactionManagement
public class BatchAdmin extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(BatchAdmin.class, args);
}
// ...
}
@Configuration @ComponentScan("com.company.package*")
@Import({ ServletConfiguration.class, WebappConfiguration.class })
public class MainConfiguration {}
@Configuration
@ImportResource("classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml")
public class ServletConfiguration {}
@Configuration
@ImportResource({ "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml"
,"classpath:persistence-context.xml" })
public class WebappConfiguration {}
在我persistence.xml
我配置了兩個數據源,使用兩個PlatformTransactionManager
實例。 顯然,spring-boot默認情況下無法處理這種情況。
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: transactionManager,osm.transactionManager
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:313)
at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:337)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:252)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
//...
任何想法如何覆蓋彈簧引導默認值,以便我可以一起使用兩個數據源?
編輯:請告訴我奇怪的是,儘管有兩個不同JpaTransactionManager
豆定義的事務方面的支持似乎沒有一個名字的錯誤是從beanFactory
的的getBean方法拋出豆
<bean id="osm.transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="osm.entityManagerFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="osm.transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="osmServiceOperation"
expression="execution(* com.company.osm.service.spec..*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="osmServiceOperation" />
</aop:config>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
:
protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) {
// ...
else if (this.transactionManagerBeanName != null) { <-- Should go here!
return this.beanFactory.getBean(this.transactionManagerBeanName, PlatformTransactionManager.class);
}
else {
return this.beanFactory.getBean(PlatformTransactionManager.class); <-- Goes here!
}
}
這兩個數據源都已在Persistence.xml中配置,該數據源已加載到我的WebbappConfiguration類中。這還不夠嗎?那麼我會試試看.. – achingfingers 2014-09-05 09:27:39
@achingfingers重要的是你如何使用豆類。如果他們按類型連接,春天有2個相同類型的豆,不知道該怎麼做。 – 2014-09-05 09:38:33