gpt4 book ai didi

java - 如何在Spring boot中禁用扫描@Configuration类

转载 作者:行者123 更新时间:2023-11-30 05:19:52 26 4
gpt4 key购买 nike

我有两个 Spring Boot 应用程序,它们依赖于同一个 common 模块。在 common 模块中,我有两个用于两个不同数据库的配置。在第一个应用程序中,我需要在 2 DB 上建立连接,但在第二个应用程序中不需要在 2 DB 上建立连接,我只想在第一个 DB 上建立连接。

我的第二个应用程序默认扫描整个common模块和Configuration以查找两个DB。我想禁用第二个配置上的扫描。我怎样才能做到这一点?

这是我的第二个应用程序:

    @SpringBootApplication(scanBasePackages = { "com" })
@EnableScheduling
public class secondApplication {
public static void main(String[] args) {
SpringApplication.run(secondApplication.class, args);
}
}

第一个配置:

    @Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "postgresqlEntityManagers",
transactionManagerRef = "postgresqlTransactionManager",
basePackages = "com.db1")
public class firstConfiguration {

第二个配置:

        @Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "postgresqlEntityManagers",
transactionManagerRef = "postgresqlTransactionManager",
basePackages = "com.db2")
public class secondConfiguration {

最佳答案

根据您的代码,您添加 @ComponnentScan 并使用 excludeFilters 来排除您想要的任何类,并通过使用将其从自动配置数据源中排除这个 @EnableAutoConfiguration 具有排除字段。通过遵循这些配置,您应该得到如下示例:

@ComponentScan(basePackages = "com.example.project",
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
classes = {AnyCustomConnectionProvider.class, AnyCustomJpaConfiguration.class}))
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})

关于java - 如何在Spring boot中禁用扫描@Configuration类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59740498/

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