我想用 EntityManager
在 SpringBoot
.
应用
@Configuration
@EnableRetry // To enable Spring retry
@EnableJpaRepositories
@EnableAspectJAutoProxy(proxyTargetClass=true)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
邮件配置
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "mailEntityManager",
transactionManagerRef = "mailTransactionManager",
basePackageClasses = MmcMonitoringLog.class)
public class MailConfig {
@Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
@Bean
@ConfigurationProperties("app.order.jpa")
public JpaProperties orderJpaProperties() {
return new JpaProperties();
}
@Bean
@ConfigurationProperties(prefix = "app.order.datasource")
public DataSource orderDataSource() {
return (DataSource) DataSourceBuilder.create().type(DataSource.class).build();
}
@Bean
public LocalContainerEntityManagerFactoryBean orderEntityManager(
JpaProperties orderJpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(orderJpaProperties);
return builder
.dataSource(orderDataSource())
.packages(MmcMonitoringLog.class)
.persistenceUnit("ordersDs")
.build();
}
@Bean
public JpaTransactionManager orderTransactionManager(EntityManagerFactory orderEntityManager) {
return new JpaTransactionManager(orderEntityManager);
}
private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties customerJpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(customerJpaProperties);
return new EntityManagerFactoryBuilder(jpaVendorAdapter,
customerJpaProperties.getProperties(), this.persistenceUnitManager);
}
private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}
}
邮件服务
public class MailService extends TaskAdaptor implements Runnable {
@Autowired
MmcMonitoringLogRepository mmcMonitoringLogRepository;
@Override
public void run() {
List<MmcMonitoringLog> list = mmcMonitoringLogRepository.findByMonitoringLog("1");
......
}
应用程序属性
spring.datasource.url= jdbc:mysql://xxxx:3306/adb?autoReconnect=true&useSSL=false
spring.datasource.username=xxx
spring.datasource.password=xxx
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = none
我试图实现以下目标https://github.com/snicoll-demos/demo-multi-entity-managers/blob/master/src/main/java/demo/order/orderconfig.java,但我的Spring Boot立即停止没有抛出任何例外。如果我删除mailconfig,springboot就可以启动了。有什么问题吗?我走的路对吗?
3条答案
按热度按时间h43kikqp1#
我认为您没有设置datasource的值。您需要在application.properties中设置“app.order.jpa”和“app.order.datasource”属性才能使示例正常工作。
关于配置属性,您可以在此处阅读:
http://www.baeldung.com/configuration-properties-in-spring-boot
请看下面的示例:
https://github.com/snicoll-demos/demo-multi-entity-managers/blob/master/src/main/resources/application.properties
gajydyqb2#
您不需要在springboot中进行bean配置,它在内部管理这种类型的配置。你可以用
EntityManagerFactory
类jpa8e2ybdfx3#
正如穆凯什·达拉吉耶的回答所说:
编辑:实际上你在使用
LocalContainerEntityManagerFactoryBean
,我刚注意到,所以下面的信息可能不适用。。在您的解决方案中,您试图创建一个bean来覆盖springboot已经自动配置的entitymanagerfactory。
我不是100%确定,但我相信要做到这一点,您需要:
排除自动配置(
@EnableJpaRepositories
在配置jpa之前可能无法工作):(也许?-未测试)设置
spring.main.allow-bean-definition-overriding=true
或者命名bean,并使用@Qualified
,以便可以重写现有的工厂bean。