【问题标题】:How to ignore one or multiple packages from setpackagestoscan while creating multiple data source using spring boot如何在使用spring boot创建多个数据源时忽略setpackagestoscan中的一个或多个包
【发布时间】:2020-11-17 16:35:37
【问题描述】:

我正在为我的项目创建两个数据源。一个用于身份验证模块,另一个用于所有其他模块。在编写配置类时,我需要提供要在两个配置类中扫描的包。在身份验证配置类中,我只提供身份验证包,而在通用配置文件中,我需要提供除身份验证之外的所有其他包。所以我需要排除身份验证模块。实际上不可能提供所有包名称来扫描实体,但仅在已开发的大型项目中除外。

这是 AuthDataSourceConfiguration 的配置类

@Configuration
@EnableTransactionManagement
//only need to scan auth repository module here for repository managment
@EnableJpaRepositories(basePackages = "com.test.multipledatasouce.auth.repo", entityManagerFactoryRef = "authEntityManagerFactory", transactionManagerRef = "authTransactionManager")
public class AuthDataSourceConfiguration {
    @Autowired
    private Environment env;

    @Bean
    @Primary
    @ConfigurationProperties("app.auth.datasource")
    public DataSourceProperties authDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.auth.datasource.configuration")
    public DataSource authDataSource() {
        return authDataSourceProperties().initializeDataSourceBuilder().type(BasicDataSource.class).build();
    }

    @Primary
    @Bean(name = "authEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean authEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(authDataSource());
        // only need to scan auth model package here for entity of auth module
        em.setPackagesToScan(new String[] { "com.test.multipledatasouce.auth.model" });
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Primary
    @Bean
    public PlatformTransactionManager authTransactionManager(
            final @Qualifier("authEntityManagerFactory") LocalContainerEntityManagerFactoryBean authEntityManagerFactory) {
        return new JpaTransactionManager(authEntityManagerFactory.getObject());
    }

这是我所有其他没有身份验证模块的模块的通用数据源配置文件

@Configuration
@EnableTransactionManagement
//here i am able to exclude auth repo and capture all other modules with help of excludeFilters
@EnableJpaRepositories(basePackages = "com.test.multipledatasouce.*", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.test.multipledatasouce.auth.*") }, entityManagerFactoryRef = "commonEntityManagerFactory", transactionManagerRef = "commonTransactionManager")
public class CommonDataSourceConfiguration {
    @Autowired
    private Environment env;

    @Bean
    @ConfigurationProperties("app.common.datasource")
    public DataSourceProperties commonDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("app.common.datasource.configuration")
    public DataSource commonDataSource() {
        return commonDataSourceProperties().initializeDataSourceBuilder().type(BasicDataSource.class).build();
    }

    @Bean(name = "commonEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean commonEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(commonDataSource());
        

> // only need to scan all other module except auth model package here
> // not able to exclude auth module        
> em.setPackagesToScan(new String[] { "com.test.multipledatasouce.*" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();

        em.setJpaPropertyMap(properties);
        return em;
    }

    @Bean
    public PlatformTransactionManager commonTransactionManager(
            final @Qualifier("commonEntityManagerFactory") LocalContainerEntityManagerFactoryBean commonEntityManagerFactory) {
        return new JpaTransactionManager(commonEntityManagerFactory.getObject());
    }

【问题讨论】:

    标签: java spring-boot jpa package datasource


    【解决方案1】:

    如果我正确理解了您的问题,您将尝试拥有 2 个软件包,其中 1 个应该经过身份验证。有多种方法可以做到这一点。

    选项1:(最简单和推荐) 创建两个springboot应用程序。一人一个。 Springboot 提供基于微服务架构的选项,有两个应用程序,理想情况下您正在计划

    1. 关注点隔离(需要验证与不需要验证)
    2. 拥有简单而具体的服务

    将两者合二为一只会使维护费用复杂化并增加。因此推荐。

    选项 2: @Springbootapplication,有一个内置的@ComponentScan。任何落入同一包结构的东西都会被自动扫描。您可以在主类中创建 2 个 bean。一种用于经过身份验证(带有组件扫描),另一种用于不经过身份验证。相应地将您的代码分成两个包。所有通用代码都可以放入 util 或 helper 包中。

    【讨论】:

      猜你喜欢
      • 2017-04-17
      • 2020-11-11
      • 2018-02-03
      • 2021-03-07
      • 1970-01-01
      • 2020-09-03
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多