在多模块SpringBoot应用程序中扫描外部jar Java数据源配置

时间:2020-10-14 03:31:23

标签: java spring spring-boot maven

我想问一下如何配置SpringBoot应用程序以包括外部Jar Java数据源配置。实体类全部在外部Jar实体类中使用@Entity进行注释,存储库通过@restore注释扩展了CrudRepository接口。 Lib模块打包为JAR文件,数据源用Java配置。 WAR模块应该导入LIB jar,并使用它从数据库中提取数据,并且数据源由JAR本身配置。

Maven构建摘要

<groupId>com.sample.app</groupId>
<artifactId>App-war</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
        <dependency>
            <groupId>com.sample.lib</groupId>
            <artifactId>Lib-jar</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.penta.account</groupId>
            <artifactId>AccountService-jar</artifactId>
            <type>jar</type>
        </dependency>
    </dependencies>

我尝试了以下方法,该方法导致实体类出现“非托管类型”错误。我知道@EnableJpaRepository和@EntityScan应该扫描要包含在WAR应用程序中的外部JAR Spring Bean。

@SpringBootApplication()
@EnableJpaRepositories({"com.sample.lib.repository","com.sample.app.repository"})
@EntityScan({"com.sample.lib.entity","com.sample.app.entity"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application .class, args);
    }
}

所以我认为我应该导入Java数据源配置,然后使用它来存储要扫描的软件包。可以编译该应用程序,但未编译WAR中的组件,从而导致Spring Boot使用默认的登录页面并忽略WAR软件包中的任何自定义身份验证。

LibDSConfig的DataSource Java配置

@Configuration
@EnableJpaRepositories("com.sample.lib.repository")
public class LibDSConfig {

    // Other @Bean classes
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(local());
        em.setPackagesToScan(new String[]{com.sample.lib.entity});
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
        return em;
    }    
}

AppDSConfig的DataSource Java配置

@Configuration
    @EnableJpaRepositories("com.sample.app.repository")
    public class AppDSConfig {
    
        // Other @Bean classes
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
            em.setDataSource(local());
            em.setPackagesToScan(new String[]{com.sample.app.entity});
            JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            em.setJpaVendorAdapter(vendorAdapter);
            em.setJpaProperties(additionalProperties());
            return em;
        }    
    }

Springboot主要Java配置

@SpringBootApplication()
@ComponentScan({"com.sample.lib.entity","com.sample.lib.entity"})
@Import({
    AppDSConfig.class,
    LibDSConfig .class
})
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我知道@ComponentScan仅按照上面的代码中的配置扫描实体类。我想问一下是否有其他解决方案可以导入外部jar java配置?

0 个答案:

没有答案