我有一个简单的Spring Boot应用程序,带有2个外部jar。每个jar都使用Spring JPA,并且完全配置为独立工作。
Jar1
JPA存储库
@Repository
public interface QuarterRepository extends JpaRepository<Quarter, Long>{
}
实体
@Entity
public class Quarter implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;...
配置
@Configuration
@Import({RepositoryConfig.class})
public class OnlineJpaAppConfig{
@Bean(name = "onlinejpaAppConfigConfigurer")
public static PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("/de/comp/onlinejpa/application.properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
@Configuration
@EnableJpaRepositories(basePackages = {"de.comp.onlinejpa.repository"},
entityManagerFactoryRef = "onlinejpaentityManagerFactory",
transactionManagerRef = "onlinejpaTransactionManager")
@EnableTransactionManagement
public class RepositoryConfig {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${hibernate.dialect}")
private String hibernateDialect;
@Value("${hibernate.show_sql}")
private String hibernateShowSql;
@Value("${hibernate.hbm2ddl.auto}")
private String hibernateHbm2ddlAuto;
private static final String QUALIFIER = "onlinejpa";
@Bean(name = "onlinejpaentityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"de.comp.onlinejpa.entity"});
em.setPersistenceUnitName("onlinejpa");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean(name = "onlinejpaDataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "onlinejpaTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(value = "onlinejpaentityManagerFactory") EntityManagerFactory onlinejpaentityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(onlinejpaentityManagerFactory);
return transactionManager;
}
@Bean(name = "onlinejpaExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", hibernateHbm2ddlAuto);
properties.setProperty("hibernate.dialect", hibernateDialect);
return properties;
}
}
第二个jar看起来和Jar1几乎相同,除了bean和包名称
@EnableJpaRepositories(basePackages = {"de.comp.aisjpa.repository"},
entityManagerFactoryRef = "aisjpaentityManagerFactory",
transactionManagerRef = "aisjpatransactionManager")
@EnableTransactionManagement
public class RepositoryConfig {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${hibernate.dialect}")
private String hibernateDialect;
@Value("${hibernate.show_sql}")
private String hibernateShowSql;
@Value("${hibernate.hbm2ddl.auto}")
private String hibernateHbm2ddlAuto;
private static final String QUALIFIER = "aisjpa";
@Bean(name = "aisjpaentityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(aisdataSource());
em.setPackagesToScan(new String[]{"de.comp.aisjpa.entity"});
em.setPersistenceUnitName("ais");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean(name = "aisjpadatasource")
public DataSource aisdataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "aisjpatransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(value = "aisjpaentityManagerFactory") EntityManagerFactory aisjpaentityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(aisjpaentityManagerFactory);
return transactionManager;
}
@Bean(name = "aisjpaexceptionTranslation")
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", hibernateHbm2ddlAuto);
properties.setProperty("hibernate.dialect", hibernateDialect);
return properties;
}
}
在Jar1中运行此Main类会得到预期的结果..(与Jar2相同)
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(OnlineJpaAppConfig.class);
QuarterRepository quarterRepository = ctx.getBean("quarterRepository", QuarterRepository.class);
List<Quarter> quarters = quarterRepository.findAll();
for (Quarter quarter : quarters) {
System.out.println(quarter.getQuarter());
}
}
}
这是我的Spring Boot应用程序的启动类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class } )
@Import({ KVLdapAppConfig.class, OnlineJpaAppConfig.class,AISAppConfig.class})
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
服务器启动时没有问题,但在配置jpa时出现了一些问题。持久性单元ais的配置应使用Oracle方言和驱动程序。似乎它选择了 Jar1 的配置。
Building JPA container EntityManagerFactory for persistence unit 'onlinejpa'
HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
HHH000412: Hibernate Core {4.2.0.Final}
HHH000206: hibernate.properties not found
HHH000021: Bytecode provider name : javassist
HHH000204: Processing PersistenceUnitInfo [
HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
HHH000397: Using ASTQueryTranslatorFactory
Loaded JDBC driver: com.mysql.jdbc.Driver
Building JPA container EntityManagerFactory for persistence unit 'ais'
HHH000204: Processing PersistenceUnitInfo [
HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
HHH000400: Using dialect: **org.hibernate.dialect.MySQL5Dialect**
HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
HHH000397: Using ASTQueryTranslatorFactory
Loaded JDBC driver: com.mysql.jdbc.Driver
下一个问题是数据库访问。 Spring不知道它应该使用哪个事务管理器
: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: aisjpatransactionManager,onlinejpaTransactionManager
将一个tx管理器设置为@Primary解决了上述问题,但以另一个lib中找不到的表结束,导致错误配置的tx管理器/持久性单元。
也许我的问题是为每个jar创建LocalContainerEntityManagerFactoryBean。相反,我应该使用以下代码来设置我的数据库连接,但这会破坏我的个人lib。
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", "create");
return builder
.dataSource(internalDataSource())
.packages("package here")
.persistenceUnit("onlinjpa")
.properties(properties)
.build();
}
如何让每个jar在Spring Boot应用程序中运行。
这是使用Sping Boot设置多个数据源的链接,但我不想在Spring Boot应用程序中配置我的libs数据源。
答案 0 :(得分:1)
问题是注入了错误的属性值,可能是由于每个jar中PropertyPlaceholderConfigurer
的静态调用引起的。
使用
@PropertySource("classpath:/de/comp/aisjpa/application.properties")
public class RepositoryConfig
@Autowired
private Environment env;
dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
....
或者将静态调用留给PropertyPlaceholderConfigurer
,但在application.properties中添加属性前缀,以便每个属性值在整个应用程序中都是唯一的
这
jdbc.driverClassName=oracle.jdbc.OracleDriver
要
jar1.jdbc.driverClassName=oracle.jdbc.OracleDriver