Spring Boot多个配置文件DataSource

时间:2016-06-24 02:16:35

标签: java oracle spring-boot yaml datasource

我正在编写一个带有两个Oracle数据源的Spring Boot应用程序 - 这是我的application.yml文件:

spring:
    profiles: test
datasource:
  app2:
       url: jdbc:oracle:thin:@//rap-vin:1521/APP221
       username: user2
       password: pwd2
       driver-class-name: oracle.jdbc.OracleDriver
  app:
       url: jdbc:oracle:thin:@//eap10-san:1522/APP22
       username: user
       password: pwd
       driver-class-name: oracle.jdbc.OracleDriver
application:
  app:
       url: example.com/app

我有一个DBConfiguration类,如下所示:

@Configuration
public class DBConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.app")
    public DataSource appDataSource() {
        DataSource ds = DataSourceBuilder.create().build();
        return ds;
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.app2")
    public DataSource app2DataSource() {
        DataSource ds = DataSourceBuilder.create().build();
        return ds;
    }
}

然后我有一个测试课:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DBConfiguration.class)
public class Test {

    private JdbcTemplate jdbcTemplateAPP;
    private JdbcTemplate jdbcTemplateAPP2;
    //private RestTemplate restTemplate;


    @Autowired
    public void setDataSource(final DataSource appDataSource,
                @Qualifier("app2DataSource") final DataSource app2DataSource) {
            this.jdbcTemplateAPP = new JdbcTemplate(appDataSource);
            this.jdbcTemplateAPP2 = new JdbcTemplate(app2DataSource);
        }

    @Test
    public void testJob() throws Exception {
        int count= jdbcTemplateAPP.queryForObject("select count(*) from parm", int.class);

        Assert.assertTrue(count==1);
    }
}

但每当我运行测试时,我都会将db urls视为null。 我究竟做错了什么?另外我应该如何阅读application.app url?感谢你的帮助 - (我是一个春天的新手) 错误:

java.sql.SQLException: The url cannot be null
    at java.sql.DriverManager.getConnection(DriverManager.java:649) ~[na:1.8.0_77]
    at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_77]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:305) ~[tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200) ~[tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:708) [tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:642) [tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:464) [tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:141) [tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115) [tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102) [tomcat-jdbc-8.0.33.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126) [tomcat-jdbc-8.0.33.jar:na]
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:390) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:480) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:490) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:496) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at com.tgt.trans.intl.batchtest.mib.tests.MIB022Test.testJob(MIB022Test.java:35) [test-classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12]
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]

2 个答案:

答案 0 :(得分:0)

第一,

你激活了个人资料&#34; test&#34;通过VM参数或以某种方式? 您的yml似乎只是定义了配置文件,但它仍未激活。

如果您想激活个人资料&#34;测试&#34;在yml中 你应该在下面写下

spring:
  profiles: 
    active: test

或将其放入VM

-Dspring.profiles.active=test

第二

在测试课程中 appDataSource和app2DataSource都将由appDataSource注入。

或许你不打算这样做 所以如果你想将app2DataSource注入app2DataSource 应写在下面

@Qualifier("app2DataSource") final DataSource app2DataSource

最后 如果你想在你的测试类中使用yml的内容(比如application.app.url) 把

@ConfigurationProperties(prefix = "application.app")

在你的类上并定义像这样的变量

private String url;

答案 1 :(得分:0)

通过编辑得到它:

@Configuration
public class DBConfiguration {

... 

为:

@SpringBootApplication
public class DBConfiguration {

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