简体   繁体   English

应用程序无法加载 application.yml 文件

[英]Application failed to load application.yml file

I am writting junit test cases, strange thing happening I have two databases我正在编写 junit 测试用例,奇怪的事情发生了我有两个数据库

  • 1) campaign (Application level) 2) test (Testing level) 1)活动(应用程序级别) 2)测试(测试级别)

My application structure is我的应用程序结构是

    App
    --> src/main/java
    --> src/main/resource
        application.yml

    --> src/test/java
        src/test/resource
        application.yml

When i ran Application and it loads src/main/resource application.yml file and store into memory and points to campaign db that works fine.当我运行Application并且它加载src/main/resource application.yml文件并存储到内存中并指向工作正常的活动数据库时。 When i run Junit test cases application load again src/main/resource application.yml rather loading src/test/resource application.yml当我运行Junit 测试用例应用程序再次加载src/main/resource application.yml而不是加载src/test/resource application.yml

When i forcefully run test cases then application points to test database and when i stop server and re-again run application it again points to test database instead of campaign database.当我强制运行测试用例时,应用程序指向测试数据库,当我停止服务器并重新运行应用程序时,它再次指向测试数据库而不是活动数据库。

application.yml for (src/main/resources) or (src/test/resources) application.yml for (src/main/resources) 或 (src/test/resources)

spring:
   profiles.active: local
   aop.proxy-target-class: true

---
spring:
  profiles: local

campaignDB:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/campaign
  username: root
  password: Admin@123

juintDB:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/test
  username: root
  password: Admin@123

Testing level configuration测试级别配置

@RunWith(SpringRunner.class)
public class TestDbConfig {
    @Autowired
    private Environment env;

    @Bean
    @Profile("local")
    public DataSource testDbdatasource() {
        org.apache.tomcat.jdbc.pool.DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();
        datasource.setDriverClassName(env.getRequiredProperty("juintDB.driverClassName"));
        datasource.setUrl(env.getRequiredProperty("juintDB.url"));
        datasource.setUsername(env.getRequiredProperty("juintDB.username"));
        datasource.setPassword(env.getRequiredProperty("juintDB.password"));
        return datasource;
    }
}

Application level configuration应用层配置

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(datasource());
    emf.setJpaVendorAdapter(hibernateJpa);
    emf.setPackagesToScan("com.brighttalk.campaign.model");

    Map<String, String> jpaSchema = new HashMap<String, String>();
    jpaSchema.put("hibernate.default_schema",
            env.getRequiredProperty("hibernate.default_schema"));

    jpaSchema.put("hibernate.dialect",
            env.getRequiredProperty("hibernate.dialect"));
    jpaSchema.put("hibernate.format_sql",
            env.getRequiredProperty("hibernate.format_sql"));
    jpaSchema.put("hibernate.hbm2ddl.auto",
            env.getRequiredProperty("hibernate.hbm2ddl.auto"));
    jpaSchema.put("hibernate.show_sql",
            env.getRequiredProperty("hibernate.show_sql"));

    emf.setJpaPropertyMap(Collections.singletonMap(
            "javax.persistence.validation.mode", "none"));
    emf.setJpaPropertyMap(jpaSchema);
    return emf;
}

I am expecting when i test cases it should points to test database.我期待当我测试用例时它应该指向测试数据库。 and when i run Application it should points to campaign database.当我运行应用程序时,它应该指向活动数据库。

Application properties are expected to be located under the main resource directory, so the behavior you're experiencing is normal.应用程序属性应位于主资源目录下,因此您遇到的行为是正常的。

To achieve what you're trying to do, you need to create a profile specific application properties, either by:要实现您要执行的操作,您需要通过以下方式创建配置文件特定的应用程序属性:

  1. Having a separate file for each profile (for instance application-test.yml for a profile named test ) and add it in the same directory.每个配置文件都有一个单独的文件(例如application-test.yml用于名为test的配置文件)并将其添加到同一目录中。
  2. Or as you did above add separate profile specific properties in the same application.yml file.或者像您在上面所做的那样,在同一个application.yml文件中添加单独的配置文件特定属性。

Then you'll need to run your integration tests with active profile test , and spring boot then will pick up the properties specific to this profile.然后您需要使用 active profile test运行集成测试,然后 spring boot 将选择特定于该配置文件的属性。

@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("test")
public class MyIntegrationTest {
    ....
}

If you do not specify and active profile then the active profile will be default which means spring boot will load the default properties.如果您没有指定和活动配置文件,那么活动配置文件将是default ,这意味着 spring boot 将加载默认属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM