gpt4 book ai didi

java - SpringBoot 多数据源 - 仅在外部 Tomcat 容器中失败

转载 作者:行者123 更新时间:2023-11-28 23:25:15 25 4
gpt4 key购买 nike

SpringBoot 多数据源 - 在外部 Tomcat 容器中失败

我们正在使用 springboot 创建两个不同的数据源 (microsoft sqlserver)。这在 eclipse Luna tomcat 容器内正常工作。当我们尝试在外部 tomcat(8.0.x) 容器中部署 war 文件时,失败并出现以下异常。

请让我们知道我们是否缺少任何配置,因为它在 eclipse 中正常工作

错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
org.springframework.boot.autoconfigure.condition.OnBeanCondition.getPrimaryBeans(OnBeanCondition.java:237)
org.springframework.boot.autoconfigure.condition.OnBeanCondition.hasSingleAutowireCandidate(OnBeanCondition.java:230)
org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:102)
org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102)
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader$TrackedConditionEvaluator.shouldSkip(ConfigurationClassBeanDefinitionReader.java:436)
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:127)
org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333)
org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:677)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:519)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)

第一个数据源:

@Configuration
@EnableJpaRepositories(basePackages = "com.example.adapter.repository.first",
entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@EntityScan("com.example.adapter.domain.first")
@EnableTransactionManagement
public class FirstDataSourceConfig {

@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return DataSourceBuilder.create().build()
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder) {
return builder
.dataSource(dataSource())
.packages("com.example.adapter.domain.first")
.persistenceUnit("first")
.build()
}

@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory){
return new JpaTransactionManager(entityManagerFactory)
}
}

第二个数据源:

@Configuration
@EnableJpaRepositories(basePackages = "com.example.adapter.repository.second",
entityManagerFactoryRef = "secondEntityManagerFactory", transactionManagerRef = "secondTransactionManager")
@EntityScan("com.example.adapter.domain.second")
@EnableTransactionManagement
public class SecondDataSourceConfig {

@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "datasource.second")
public DataSource secondDataSource(){
return DataSourceBuilder.create().build()
}

@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(final EntityManagerFactoryBuilder builder){
return builder
.dataSource(secondDataSource())
.packages("com.example.adapter.domain.second")
.persistenceUnit("secondPersistenceUnit")
.build()
}

@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(@Qualifier("secondEntityManagerFactory") final EntityManagerFactory factory) {
return new JpaTransactionManager(factory)
}
}

属性:

spring.datasource.url: jdbc:sqlserver://XXXXX:1433;databaseName=XXXXX
spring.datasource.username: XXXXXX
spring.datasource.password: XXXXXX
spring.datasource.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

datasource.second.url: jdbc:sqlserver://XXXXXX:1433;databaseName=XXXXXX
datasource.second.username: XXXXXX
datasource.second.password: XXXXXX
datasource.second.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

spring.jpa.properties.hibernate.show_sql: false
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.SQLServerDialect

主要应用:

@EnableCaching
@CompileStatic
@EnableScheduling
@SpringBootApplication
@EnableAutoConfiguration(exclude=[MultipartAutoConfiguration.class])
class MainApplication extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MainApplication.class);
}

static void main(String[] args) throws Exception {
SpringApplication.run(MainApplication.class, args)
}

}

build.xml(Gradle Build变化-外部war部署)

apply plugin: 'war'

providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

configurations {
providedRuntime
}

最佳答案

问题已通过删除应用程序中@EnableAutoConfiguration 的多次使用得到修复。我们注意到 @EnableAutoConfiguration 已在 Swaggerconfig 文件中与 MainApplication.java 一起使用,我们在 Swaggerconfig 文件中将其删除。

现在应用程序在本地和外部 tomcat 中正常工作。

关于java - SpringBoot 多数据源 - 仅在外部 Tomcat 容器中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com