我有一个Spring引导项目,从导入的jar中提取数据库配置,并尝试连接自己的数据库配置以进行第二次数据库连接。 jar中的第一个DB配置标有@Primary
,第二个不是。
当我尝试启动项目时,我收到此错误。
Not a managed type: class test.example.MyEntity
在我的第一个数据库配置中,它正在寻找一个完全不同的包,我们称之为com.example.test
,这是这样配置的:
@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:/${app.execution.environment}/application.properties" })
@EnableJpaRepositories(basePackages = "com.example.test", entityManagerFactoryRef = "mysqlEntityManager", transactionManagerRef = "mysqlTransactionManager")
public class MysqlHibernateConfig {
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean mysqlEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(mysqlDataSource());
em.setPackagesToScan(new String[] { "com.example.test" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaPropertyMap(hibernateProperties());
return em;
}
.... other relevant methods below for configuring DB
}
在我的第二个数据库配置中,我指向一个完全不同的实体包
@Configuration
@EnableTransactionManagement
@EntityScan(basePackages = { "test.example" })
@EnableJpaRepositories(entityManagerFactoryRef = "myEntityManager", transactionManagerRef = "myTransactionManager", basePackages = {
"test.example" })
public class MyRepositoryConfig {
@Bean
public LocalContainerEntityManagerFactoryBean myEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(vspnetDataSource());
em.setPackagesToScan(new String[] { "test.example" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaPropertyMap(vspnetHibernateProperties());
return em;
}
}
我不明白为什么它不会找到并管理测试中的bean。例如,这些实体看起来像这样:
@Entity
@Table(name = "employee")
public class MyEntity {
// column fields
}
我的配置如下:
@Configuration
@ComponentScan(basePackages = {"com.example.test", "test.example"})
public class WebMvcConfig extends WebMvcConfigurerAdapter {
}
如何让spring正确管理这些bean并允许我使用这些实体?
我检查了所有托管bean,发现我的第二个实体经理出现了我的第一个,所以我知道他们正在接线。不知道为什么他们不会抓住正确的实体。
完整堆栈跟踪
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
2017-07-21 10:06:57,989 INFO application - Starting application on MacBook-Pro-2.local with PID 5694 (targetclasses started by stevmc in /Users/stevmc/git/oos-admin-ng2) [tx-id=]
2017-07-21 10:06:57,992 INFO application - No active profile set, falling back to default profiles: default [tx-id=]
2017-07-21 10:06:58,039 INFO AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12b0404f: startup date [Fri Jul 21 10:06:58 PDT 2017]; root of context hierarchy [tx-id=]
2017-07-21 10:06:59,564 INFO AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring [tx-id=]
2017-07-21 10:06:59,728 INFO PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$b8cd37e5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [tx-id=]
2017-07-21 10:06:59,785 INFO AnnotationActionEndpointMapping - Supporting [WS-Addressing August 2004, WS-Addressing 1.0] [tx-id=]
2017-07-21 10:06:59,825 INFO PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$143572e6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [tx-id=]
2017-07-21 10:06:59,954 INFO PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'oos-member-ecom-api-dep-config' of type [com.example.oos.member.config.EcomEligibilityServiceConfig$$EnhancerBySpringCGLIB$$20a01c0c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [tx-id=]
2017-07-21 10:07:00,253 INFO TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8080 (http) [tx-id=]
2017-07-21 10:07:00,261 INFO StandardService - Starting service Tomcat [tx-id=]
2017-07-21 10:07:00,261 INFO StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.11 [tx-id=]
2017-07-21 10:07:00,335 INFO [/] - Initializing Spring embedded WebApplicationContext [tx-id=]
2017-07-21 10:07:00,335 INFO ContextLoader - Root WebApplicationContext: initialization completed in 2300 ms [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'metricsFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'corsFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*] [tx-id=]
2017-07-21 10:07:00,716 INFO ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/] [tx-id=]
2017-07-21 10:07:00,717 INFO ServletRegistrationBean - Mapping servlet: 'messageDispatcherServlet' to [/services/*] [tx-id=]
2017-07-21 10:07:00,756 INFO DriverManagerDataSource - Loaded JDBC driver: net.sourceforge.jtds.jdbc.Driver [tx-id=]
2017-07-21 10:07:00,835 INFO LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-21 10:07:00,844 INFO LogHelper - HHH000204: Processing PersistenceUnitInfo [
name: default
...] [tx-id=]
2017-07-21 10:07:00,885 INFO Version - HHH000412: Hibernate Core {5.0.12.Final} [tx-id=]
2017-07-21 10:07:00,887 INFO Environment - HHH000206: hibernate.properties not found [tx-id=]
2017-07-21 10:07:00,887 INFO Environment - HHH000021: Bytecode provider name : javassist [tx-id=]
2017-07-21 10:07:00,913 INFO Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final} [tx-id=]
2017-07-21 10:07:01,035 INFO Dialect - HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect [tx-id=]
2017-07-21 10:07:01,072 INFO LobCreatorBuilderImpl - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 [tx-id=]
2017-07-21 10:07:01,250 WARN RootClass - HHH000038: Composite-id class does not override equals(): test.steven.EmployeeGroupEntity$EmployeeGroupId [tx-id=]
2017-07-21 10:07:01,250 WARN RootClass - HHH000039: Composite-id class does not override hashCode(): test.steven.EmployeeGroupEntity$EmployeeGroupId [tx-id=]
2017-07-21 10:07:01,420 INFO LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-21 10:07:01,448 INFO DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver [tx-id=]
2017-07-21 10:07:01,526 INFO LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-21 10:07:01,527 INFO LogHelper - HHH000204: Processing PersistenceUnitInfo [
name: default
...] [tx-id=]
2017-07-21 10:07:01,697 INFO Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect [tx-id=]
2017-07-21 10:07:01,927 WARN EntityManagerFactoryRegistry - HHH000436: Entity manager factory name (default) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name' [tx-id=]
2017-07-21 10:07:01,927 INFO LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-21 10:07:02,309 WARN AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [targetclasses/com/example/oos/admin/controller/UserController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeService' defined in file [targetclasses/com/example/oos/admin/service/EmployeeService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class test.steven.EmployeeEntity [tx-id=]
2017-07-21 10:07:02,309 INFO LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-21 10:07:02,310 INFO LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-21 10:07:02,311 INFO StandardService - Stopping service Tomcat [tx-id=]
2017-07-21 10:07:02,321 INFO AutoConfigurationReportLoggingInitializer -
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. [tx-id=]
2017-07-21 10:07:02,329 ERROR SpringApplication - Application startup failed [tx-id=]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [targetclasses/com/example/oos/admin/controller/UserController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeService' defined in file [targetclasses/com/example/oos/admin/service/EmployeeService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class test.steven.EmployeeEntity
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.example.oos.admin.application.main(application.java:10)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeService' defined in file [targetclasses/com/example/oos/admin/service/EmployeeService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class test.steven.EmployeeEntity
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 19 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class test.steven.EmployeeEntity
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 33 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class test.steven.EmployeeEntity
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:153)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:100)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:82)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 44 common frames omitted
Process finished with exit code 1