相关疑难解决方法(0)

使用YAML的Spring @PropertySource

Spring Boot允许我们用YAML等价物替换我们的application.properties文件.但是我的测试似乎遇到了麻烦.如果我注释我的TestConfiguration(一个简单的Java配置),它期望一个属性文件.

例如,这不起作用: @PropertySource(value = "classpath:application-test.yml")

如果我在我的YAML文件中有这个:

db:
  url: jdbc:oracle:thin:@pathToMyDb
  username: someUser
  password: fakePassword
Run Code Online (Sandbox Code Playgroud)

我会用这样的东西来利用这些价值观:

@Value("${db.username}") String username
Run Code Online (Sandbox Code Playgroud)

但是,我最终得到了错误:

Could not resolve placeholder 'db.username' in string value "${db.username}"
Run Code Online (Sandbox Code Playgroud)

我如何在测试中利用YAML的优点?

spring spring-boot

89
推荐指数
8
解决办法
9万
查看次数

如何使用YamlPropertiesFactoryBean使用Spring Framework 4.1加载YAML文件?

我有一个当前使用*.properties文件的spring应用程序,我希望使用YAML文件.

我发现类YamlPropertiesFactoryBean似乎能够做我需要的.

我的问题是我不确定如何在我的Spring应用程序(使用基于注释的配置)中使用此类.看来我应该使用 setBeanFactory方法在PropertySourcesPlaceholderConfigurer中配置它.

以前我使用@PropertySource加载属性文件,如下所示:

@Configuration
@PropertySource("classpath:/default.properties")
public class PropertiesConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在PropertySourcesPlaceholderConfigurer中启用YamlPropertiesFactoryBean以便我可以直接加载YAML文件?还是有另一种方法吗?

谢谢.

我的应用程序使用基于注释的配置,我使用的是Spring Framework 4.1.4.我找到了一些信息,但它始终指向Spring Boot,就像一样.

java spring yaml properties-file

35
推荐指数
3
解决办法
6万
查看次数

标签 统计

spring ×2

java ×1

properties-file ×1

spring-boot ×1

yaml ×1