我在PersistanceConfig.class
中有以下代码在SpringBoot 1.5中正常工作:
@Configuration
@EnableJpaRepositories(basePackages = {
"somePackagesName.repository",
"somePackagesName2.repository",
"somePackagesName3.repository"
})
class PersistanceConfig extends MultitenancyConfiguration {
@Override
protected Class<?>[] entityManagerPackages() {
return ImmutableList.of(
Jsr310JpaConverters.class,
CalculationDtoJpaConverter.class,
CommonModel.class,
Model.class,
somePackagesName.Model.class,
somePackagesName.model.Model.class)
.toArray(new Class[]{});
}
}
以相同的方式运行时(我以前在Tomcat 8.5上运行过)-现在出现以下错误:
18-Oct-2018 13:18:51.798 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/policyManagement]]
(...)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource
[somePackagesName/PersistanceConfig.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 3;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'jpaVendorAdapter' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception;
nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
(...)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder'
defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter'
threw exception; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
(...)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter'
defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
(...)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]:
Factory method 'jpaVendorAdapter' threw exception; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
(...)
Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
(...)
如何更改此代码,使其在SpringBoot 2.x中可以正常工作?
这里是使用entityManagerPackages
的类:
public abstract class MultitenancyConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix = "primary.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "secondary.datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "default.datasource")
public DataSource defaultDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
MultiTenantConnectionProvider multiTenantConnectionProvider,
CurrentTenantIdentifierResolver currentTenantIdentifierResolver,
EntityManagerFactoryBuilder builder) {
Map<String, Object> props = new HashMap<>();
props.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
props.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
props.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);
return builder
.dataSource(dataSource)
.properties(props)
.packages(entityManagerPackages())
.build();
}
protected abstract Class<?>[] entityManagerPackages();
@Bean
public FilterRegistrationBean myFilter(AutowireCapableBeanFactory beanFactory) {
FilterRegistrationBean registration = new FilterRegistrationBean();
Filter tenantFilter = new MultiTenantFilter();
beanFactory.autowireBean(tenantFilter);
registration.setFilter(tenantFilter);
registration.addUrlPatterns("/*");
return registration;
}
}