030.External Properties - dkkahm/study-springfamework5 GitHub Wiki

External Properties

  • Properties file (resources/datasource.properties)
guru.username=John
guru.password=somepass
guru.dburl=adkljdljgdkf
  • POJO for properties (FakeDataSource)
public class FakeDataSource {
    private String user;
    private String password;
    private String url;
    ....

Properties Loader

  • Since Spring 2.x
@Configuration
@PropertySource("classpath:datasource.properties")
public class PropertyConfig {

    @Value("${guru.username}")
    String user;

    @Value("${guru.password}")
    String password;

    @Value("${guru.dburl}")
    String url;

    @Bean
    public FakeDataSource fakeDataSource() {
        FakeDataSource fakeDataSource = new FakeDataSource();
        fakeDataSource.setUser(user);
        fakeDataSource.setPassword(password);
        fakeDataSource.setUrl(url);;
        return fakeDataSource;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        return propertySourcesPlaceholderConfigurer;
    }
}
  • @PropertySource with file
@PropertySource("file:/etc/properties/common/common.properties")

With Environment

  • Since 3.x
@Configuration
@PropertySource("classpath:datasource.properties")
public class DataSourceConfiguration {
    @Autowired
    Environment env;

    @Bean
    public FakeDataSource fakeDataSource() {
        FakeDataSource dataSource = new FakeDataSource();
        dataSource.setUser(env.getProperty("guru.username"));
        dataSource.setPassword(env.getProperty("guru.password"));
        dataSource.setUrl(env.getProperty("guru.dburl"));

        return dataSource;
    }
}
  • properties can be overriden with environment variables

Configuration with multiple property files

  • PropertySource
@PropertySource({"classpath:datasource.properties", "classpath:jms.properties"})
  • PropertySources
@PropertySources({
    @PropertySource("classpath:datasource.properties"),
    @PropertySource("classpath:jms.properties")
})

Properties with Profile

  • application-{profile}.properties
  • with active profile, application-{profile}.properties override application.properties

YML with multiple profile

  • application.yml
spring:
    profiles:
        active: de
---
spring:
    profiles: de
guru:
    jms:
        username: JMS Username $$$$ German
---
spring:
    profiles: es
guru:
    jms:
        username: JMS Username $$$$ Spanish

Properties in YML

  • define YamlPropertySourceFactory
public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)
            throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }
}
  • annotate configuration to use yml
@Configuration
@ConfigurationProperties(prefix = "yaml")
@PropertySource(value = "classpath:datasource.yml", factory = YamlPropertySourceFactory.class)
public class DataSourceConfiguration {