当我尝试在 Spring boot 中编写具有两个数据库的应用程序时,Spring Data JPA。它无法创建数据库,这是给定的例外情况。这是我的实现代码
应用类
package com.icarat.eshiksha;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
application.xml 文件
server.port=8080
server.contextPath=/Eshiksha
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_shiksha2
spring.datasource.username=root
spring.datasource.password=*****
spring.datasource.validation-query=select 1
settings.datasource.driver-class-name=com.mysql.jdbc.Driver
settings.datasource.url=jdbc:mysql://127.0.0.1:3306/db_shiksha_settings2
settings.datasource.username=root
settings.datasource.password=*******
settings.datasource.validation-query=select 1
Run Code Online (Sandbox Code Playgroud)
ShikshaDbConfig 类
package com.icarat.eshiksha.config;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "shikshaEntityManagerFactory",
transactionManagerRef = "shikshaTransactionManager",
basePackages = { "com.icarat.eshiksha.repository" })
public class ShikshaDbConfig {
@Autowired
JpaVendorAdapter jpaVendorAdapter;
@Autowired
DataSource dataSource;
@Bean(name = "shikshaManager")
public EntityManager shikshaManager() {
return shikshaEntityManagerFactory().createEntityManager();
}
@Primary
@Bean(name = "shikshaEntityManagerFactory")
public EntityManagerFactory shikshaEntityManagerFactory() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto","update");
properties.setProperty("hibernate.show_sql", "false");
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource);
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setJpaProperties(properties);
emf.setPackagesToScan("com.icarat.eshiksha.database.entities");
emf.setPersistenceUnitName("default"); // <- giving 'default' as name
emf.afterPropertiesSet();
return emf.getObject();
}
@Bean(name = "shikshaTransactionManager")
public PlatformTransactionManager shikshaTransactionManager() {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(shikshaEntityManagerFactory());
return tm;
}
}
Run Code Online (Sandbox Code Playgroud)
ShikshaSettingsDbConfig 类
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "settingEntityManagerFactory",
transactionManagerRef = "shikshaSettingsTransactionManager",
basePackages = { "com.icarat.eshiksha.settings.repository" })
public class ShikshaSettingsDbConfig {
@Autowired
JpaVendorAdapter jpaVendorAdapter;
@Value("${settings.datasource.url}")
private String databaseUrl;
@Value("${settings.datasource.username}")
private String username;
@Value("${settings.datasource.password}")
private String password;
@Value("${settings.datasource.driver-class-name}")
private String driverClassName;
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseUrl, username, password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
@Bean(name = "settingEntityManager")
public EntityManager settingEntityManager() {
return settingEntityManagerFactory().createEntityManager();
}
@Bean(name = "settingEntityManagerFactory")
public EntityManagerFactory settingEntityManagerFactory() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto","update");
properties.setProperty("hibernate.show_sql", "false");
properties.setProperty("hibernate.cache.use_second_level_cache", "true");
properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setPackagesToScan("com.icarat.eshiksha.settings.database.entites"); // <- package for entities
emf.setPersistenceUnitName("settingPersistenceUnit");
emf.setJpaProperties(properties);
emf.afterPropertiesSet();
return emf.getObject();
}
@Bean(name = "settingsTransactionManager")
public PlatformTransactionManager settingsTransactionManager() {
return new JpaTransactionManager(settingEntityManagerFactory());
}
}
Run Code Online (Sandbox Code Playgroud)
组织类
@Entity
@Table(name = "Organization")
public class Organization {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="orgId")
private String orgId;
@Column(name="orgName", unique=true)
private String orgName;
@Column(name="orgAddress")
private String orgAddress;
@Column(name="pincode")
private String pincode;
@Column(name="boardOfEducation")
private String boardOfEducation;
@Column(name="recognizedBy")
private String recognizedBy;
@Column(name="affiliationNumber")
private String affiliationNumber;
@Column(name="faxNumber")
private String faxNumber;
@Column(name = "isActive")
private boolean isActive = true;
@OneToOne
private OrganizationAdmin admin;
@OneToMany(fetch = FetchType.LAZY, mappedBy="org", cascade=CascadeType.REMOVE)
private List<Branch> branches =new ArrayList<Branch>();
//getter //setter
}
Run Code Online (Sandbox Code Playgroud)
组织DAOImpl类
package com.icarat.eshiksha.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.icarat.eshiksha.dao.OrganizationDAO;
import com.icarat.eshiksha.database.entities.Organization;
import com.icarat.eshiksha.dto.AddOrgRequestDTO;
import com.icarat.eshiksha.repository.OrganizationRepository;
import com.icarat.eshiksha.util.StringConstants;
@Service
public class OrganizationDAOImpl implements OrganizationDAO {
@Autowired
private OrganizationRepository organizationRepository;
@Override
public String addOrganization(AddOrgRequestDTO request) {
Organization org = createHomeEntity(request);
try {
organizationRepository.save(org);
return StringConstants.SUCCESS;
}catch(DataIntegrityViolationException e) {
e.printStackTrace();
return null;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
private Organization createHomeEntity(final AddOrgRequestDTO request) {
Organization home = new Organization();
home.setOrgName(request.getOrgName());
home.setOrgAddress(request.getAddress());
home.setPincode(request.getPincode());
if(request.getFaxNumber()!=null){
home.setFaxNumber(request.getFaxNumber());
}
if(request.getBoardOfEducation()!=null){
home.setBoardOfEducation(request.getBoardOfEducation());
}
if(request.getRecognizedBy()!=null){
home.setRecognizedBy(request.getRecognizedBy());
}
if(request.getAffiliationNumber()!=null){
home.setAffiliationNumber(request.getAffiliationNumber());
}
return home;
}
Run Code Online (Sandbox Code Playgroud)
组织存储库类
package com.icarat.eshiksha.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.icarat.eshiksha.database.entities.Organization;
@Repository
public interface OrganizationRepository extends JpaRepository<Organization, String>{
}
Run Code Online (Sandbox Code Playgroud)
抛出的异常有
2017-05-28 16:08:16.061 WARN 4424 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'organizationDAOImpl': Unsatisfied dependency expressed through field 'organizationRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settingEntityManagerFactory' defined in class path resource [com/icarat/eshiksha/config/ShikshaSettingsDbConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'settingEntityManagerFactory' threw exception; nested exception is java.lang.AbstractMethodError
2017-05-28 16:08:16.070 INFO 4424 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-05-28 16:08:16.075 ERROR 4424 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'organizationDAOImpl': Unsatisfied dependency expressed through field 'organizationRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settingEntityManagerFactory' defined in class path resource [com/icarat/eshiksha/config/ShikshaSettingsDbConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'settingEntityManagerFactory' threw exception; nested exception is java.lang.AbstractMethodError
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at com.icarat.eshiksha.Application.main(Application.java:38) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settingEntityManagerFactory' defined in class path resource [com/icarat/eshiksha/config/ShikshaSettingsDbConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'settingEntityManagerFactory' threw exception; nested exception is java.lang.AbstractMethodError
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
... 19 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settingEntityManagerFactory' defined in class path resource [com/icarat/eshiksha/config/ShikshaSettingsDbConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'settingEntityManagerFactory' threw exception; nested exception is java.lang.AbstractMethodError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.ja
我在这里创建了一个工作POC。README.md 中提供了说明。该解决方案使用 Gradle 进行构建,并使用 Docker 来保留两个 MySQL 实例和一个公开 REST 端点的 SpringBoot 应用程序。
关于您的实施的一些要点:
1.使用唯一的bean名称
您定义了与两个数据源相对应的两个配置。您需要为每个 bean 命名,@Bean("name")并按名称注入它们@Qualifier("name")。
另外,其中一个配置需要用@Primaryelse 注释其 bean,这将不起作用,不要问我为什么。如果您有多个相同类型的 bean 通过类型注入而没有任何进一步的限定,那么接收错误是正常的,但即使注入是用@Qualifier("name"), where"name"是唯一的限定的,似乎也会出现错误。
例如,其中一种配置可能如下所示:
package my.java.spring.jpa.multi.repositories.config;
import my.java.spring.jpa.multi.models.Organization;
import my.java.spring.jpa.multi.repositories.source1.Source1OrganizationRepositoryImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
/**
* Repository configuration.
* @EnableJpaRepositories to enable JPA automatic repositories.
*/
@Configuration
@EnableJpaRepositories(
basePackageClasses = { Source1OrganizationRepositoryImpl.class },
entityManagerFactoryRef = MySqlSource1Config.EntityManagerFactoryBeanName,
transactionManagerRef = MySqlSource1Config.TransactionManagerBeanName
)
public class MySqlSource1Config {
public static final String PrefixName = "source1";
public static final String DataSourceBeanName = PrefixName + ".data-source";
public static final String JpaVendorAdapterBeanName = PrefixName + "jpa-vendor-adapter";
public static final String EntityManagerFactoryBeanName = PrefixName + ".entity-manager-factory";
public static final String TransactionManagerBeanName = PrefixName + ".transaction-manager";
public static final String RepositoryBeanName = PrefixName + ".repository";
@Bean(TransactionManagerBeanName)
@Primary
public PlatformTransactionManager transactionManager(
@Qualifier(EntityManagerFactoryBeanName) EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean(EntityManagerFactoryBeanName)
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Qualifier(DataSourceBeanName) DataSource dataSource,
@Qualifier(JpaVendorAdapterBeanName) JpaVendorAdapter vendorAdapter) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
entityManagerFactoryBean.setJpaDialect(new HibernateJpaDialect());
entityManagerFactoryBean.setPackagesToScan(Organization.class.getPackage().getName());
entityManagerFactoryBean.setPersistenceUnitName("mysqlsource1");
entityManagerFactoryBean.afterPropertiesSet();
return entityManagerFactoryBean;
}
@Bean(JpaVendorAdapterBeanName)
@Primary
@ConfigurationProperties(prefix = "source1.mysql.jpa")
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean(DataSourceBeanName)
@Primary
@ConfigurationProperties(prefix = "source1.mysql.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
Run Code Online (Sandbox Code Playgroud)
2.通过bean名称注入bean
因为同一类型有多个 bean 定义,所以您应该通过名称注入 bean @Qualifier():
@Bean(TransactionManagerBeanName)
@Primary
public PlatformTransactionManager transactionManager(
@Qualifier(EntityManagerFactoryBeanName) EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
Run Code Online (Sandbox Code Playgroud)
相反,以下内容是从您的代码中提取的:
@Bean(name = "shikshaTransactionManager")
public PlatformTransactionManager shikshaTransactionManager() {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(shikshaEntityManagerFactory());
return tm;
}
Run Code Online (Sandbox Code Playgroud)
这是不行的,因为shikshaEntityManagerFactory()会创建一个新的实例,EntityManagerFactory而不是注入现有的 bean。
3. 无需明确定义EntityManager何时EntityManagerFactory
以下内容摘自您的问题:
@Bean(name = "shikshaManager")
public EntityManager shikshaManager() {
return shikshaEntityManagerFactory().createEntityManager();
}
@Primary
@Bean(name = "shikshaEntityManagerFactory")
public EntityManagerFactory shikshaEntityManagerFactory() { ... }
Run Code Online (Sandbox Code Playgroud)
一点是两者都已定义,另一点是,如果您仍然需要EntityManager,那么您应该注入EntityManagerFactorybean,而不是执行shikshaEntityManagerFactory()生成另一个实例的方法调用EntityManagerFactory。
4. 简化配置
您可以使用@ConfigurationProperties而不是@Value将多个配置属性加载到您的 bean 中,例如:
@Bean(JpaVendorAdapterBeanName)
@Primary
@ConfigurationProperties(prefix = "source1.mysql.jpa")
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean(DataSourceBeanName)
@Primary
@ConfigurationProperties(prefix = "source1.mysql.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
Run Code Online (Sandbox Code Playgroud)
然后application.yml两个数据源的结果将如下所示:
source1:
mysql:
jpa:
show-sql: true
generate-ddl: true
database: MYSQL
datasource:
platform: postgres
url: jdbc:mysql://localhost:3307/test1
username: myuser1
password: mypass1
driverClassName: com.mysql.cj.jdbc.Driver
source2:
mysql:
jpa:
show-sql: true
generate-ddl: true
database: MYSQL
datasource:
platform: postgres
url: jdbc:mysql://localhost:3308/test2
username: myuser2
password: mypass2
driverClassName: com.mysql.cj.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
请注意 被jpa加载到JpaVendorAdapter和datasource中DataSource。
5. 保持数据源存储库分离
如果您需要对多个数据源使用相同的实体和存储库,那么您可以保留通用接口OrganizationRepository并拥有单独的接口Source1OrganizationRepository和 IMPL Source1OrganizationRepositoryImpl。
另一方面,如果您有多个数据源不使用的实体和存储库(例如:由源 1 管理的组织和由源 2 管理的用户),那么将会有由不同源管理的不同包,但不需要通用的包界面。
这允许将存储库 bean 命名为:
@Repository(MySqlSource1Config.RepositoryBeanName)
public interface Source1OrganizationRepository extends OrganizationRepository {
}
Run Code Online (Sandbox Code Playgroud)
为了与其他数据源存储库区分开来。
这些已配置,就像您已经在配置中使用它们一样(请注意basePackageClasses):
@Configuration
@EnableJpaRepositories(
basePackageClasses = { Source1OrganizationRepositoryImpl.class },
entityManagerFactoryRef = MySqlSource1Config.EntityManagerFactoryBeanName,
transactionManagerRef = MySqlSource1Config.TransactionManagerBeanName
)
public class MySqlSource1Config { ... }
Run Code Online (Sandbox Code Playgroud)
然后,可以将这些存储库进一步注入到控制器、服务或任何其他组件中,例如:
@RestController
@RequestMapping("/v1/organization")
public class OrganizationController {
private final OrganizationRepository organizationRepositoryFromSource1;
private final OrganizationRepository organizationRepositoryFromSource2;
@Autowired
public OrganizationController(
@Qualifier(MySqlSource1Config.RepositoryBeanName) OrganizationRepository organizationRepositoryFromSource1,
@Qualifier(MySqlSource2Config.RepositoryBeanName) OrganizationRepository organizationRepositoryFromSource2) {
this.organizationRepositoryFromSource1 = organizationRepositoryFromSource1;
this.organizationRepositoryFromSource2 = organizationRepositoryFromSource2;
}
...
}
Run Code Online (Sandbox Code Playgroud)
相同的实体可以与多个数据源一起使用,并由 加载EntityManager,并且可以在 内进行配置EntityManagerFactory:
@Bean(EntityManagerFactoryBeanName)
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Qualifier(DataSourceBeanName) DataSource dataSource,
@Qualifier(JpaVendorAdapterBeanName) JpaVendorAdapter vendorAdapter) {
...
entityManagerFactoryBean.setPackagesToScan(Organization.class.getPackage().getName());
...
}
Run Code Online (Sandbox Code Playgroud)
注意:在application.yml中application-docker.yml我使用了source1.mysql.jpa.generate-ddl: true. 这意味着与所找到的实体对应的表和模式EntityManager将在 MySQL 数据库中自动创建。如果您通过其他方式管理数据库架构,例如创建脚本并使用FlyWay或迁移它们Liquidbase,那么您可能需要将该选项转为false以便采用您自己的流程。
| 归档时间: |
|
| 查看次数: |
11594 次 |
| 最近记录: |