在多模块SpringBoot应用程序中扫描外部jar java数据源配置
我想询问如何配置SpringBoot应用程序以包括外部Jar Java数据源配置。实体类在外部Jar实体类中都用@Entity注释,存储库接口用@repository注释和Crudepository扩展。Lib模块打包为一个JAR文件,数据源用Java配置。WAR模块应该导入LIB-jar,并使用它从数据库中提取数据,数据源配置为jar本身 Maven构建提取在多模块SpringBoot应用程序中扫描外部jar java数据源配置,java,spring,spring-boot,maven,Java,Spring,Spring Boot,Maven,我想询问如何配置SpringBoot应用程序以包括外部Jar Java数据源配置。实体类在外部Jar实体类中都用@Entity注释,存储库接口用@repository注释和Crudepository扩展。Lib模块打包为一个JAR文件,数据源用Java配置。WAR模块应该导入LIB-jar,并使用它从数据库中提取数据,数据源配置为jar本身 Maven构建提取 <groupId>com.sample.app</groupId> <artifactId>App-
<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>
所以我认为我应该导入Java数据源配置,然后使用它来存储要扫描的包。可以编译应用程序,但WAR中的组件未编译,导致spring引导使用默认登录页,并忽略WAR包中的任何自定义身份验证
LibDSConfig的数据源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的数据源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配置?能否从应用程序中删除ComponentScan并查看是否工作正常?@sonus21如果我删除ComponentScan,Springboot将不会处理java数据源配置文件中的EnableJpaRepositories。如果我在springboot主应用程序中添加EnableJpaREpositories,它将不会抛出托管类型错误。@sonus21我刚刚发现我忘记在maven中导入模块的问题。无论如何,谢谢你的帮助!!干杯
@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);
}
}