spring boot应用程序启动失败,有两个数据源

时间:2016-08-16 22:56:18

标签: spring spring-boot spring-data datasource

我在Spring Boot应用程序中为datasources使用了两个不同的MySQL。容器无法启动时出现以下错误。我还尝试将其中一个数据源切换为H2。我仍然得到同样的错误。

完整的源代码可用here

的src /主/爪哇/ application.properties

#datasource
spring.datasource.username=test
spring.datasource.url=jdbc:mysql://localhost:3306/user_table
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


##secondary Datasource
#spring.sec.datasource.username=test
#spring.sec.datasource.url=jdbc:mysql://localhost:3306/comp_table
#spring.sec.datasource.password=test
#spring.sec.datasource.driver-class-name=com.mysql.jdbc.Driver

#secondary Datasource
spring.sec.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.sec.datasource.driverClassName=org.h2.Driver
spring.sec.datasource.username=sa
spring.sec.datasource.password=
spring.sec.jpa.database-platform=org.hibernate.dialect.H2Dialect

的src /主/ JAVA / COM /公司/富/配置/ DBConfig.java

@Configuration
public class DBConfig {


    @Bean(name="priDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource priDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "secDataSource")
    @ConfigurationProperties(prefix="spring.sec.datasource")
    public DataSource secDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcPriTemplate")
    @Autowired
    public JdbcTemplate jdbcPriTemplate(@Qualifier("priDataSource") DataSource hostds) {
        return new JdbcTemplate(hostds);
    }

    @Bean(name = "jdbcSecTemplate")
    @Autowired
    public JdbcTemplate jdbcSecTemplate(@Qualifier("secDataSource") DataSource secDataSource) {
        return new JdbcTemplate(secDataSource);
    }

}

的src /主/ JAVA / COM /公司/富/ Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

错误

2016-08-16 15:37:28.186  WARN 22600 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
2016-08-16 15:37:28.192  INFO 22600 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2016-08-16 15:37:28.212  INFO 22600 --- [           main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)


2016-08-16 15:37:28.240 ERROR 22600 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) ~[spring-context-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) ~[spring-context-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar!/:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar!/:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar!/:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar!/:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar!/:1.4.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar!/:1.4.0.RELEASE]
        at com.company.foo.Application.main(Application.java:10) [classes!/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [sample-proj-1.0.0-SNAPSHOT.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [sample-proj-1.0.0-SNAPSHOT.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [sample-proj-1.0.0-SNAPSHOT.jar:na]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58) [sample-proj-1.0.0-SNAPSHOT.jar:na]
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:603) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:443) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:424) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:310) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        ... 24 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
        at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187) ~[spring-core-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:600) ~[spring-orm-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
        ... 31 common frames omitted

1 个答案:

答案 0 :(得分:2)

错误表明它在制作EntityManagerFactory时遇到问题,但您没有为每个数据源明确创建。如果您查看多个数据源的Spring Data示例,您可以看到它们如何概述创建不同的数据源。

https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources