yingzhuo / spring-boot-stater-env

spring boot starter for yaml/toml/hocon property-source

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JDK Build Maven Central

spring-boot-starter-env

Requires

  • Java 8+
  • SpringBoot 2.2.0+

Download

  • maven / gradle is highly recommended.
<dependency>
    <groupId>com.github.yingzhuo</groupId>
    <artifactId>spring-boot-stater-env</artifactId>
    <version>2.0.3</version>
</dependency>

Usage

(1) use toml file as property source

@Configuration
@PropertySource(value = "classpath:/config.toml", factory = TomlPropertySourceFactory.class)
public class ApplicationConfig {
    // ...
}

(2) use yaml file as property source

@Configuration
@PropertySource(value = "classpath:/config.yaml", factory = YamlPropertySourceFactory.class)
public class ApplicationConfig {
    // ...
}

(3) use hocon file as property source (highly recommended)

@Configuration
@PropertySource(value = "classpath:/config.conf", factory = HoconPropertySourceFactory.class)
public class ApplicationConfig {
    // ...
}

(4) use AbstractConventionEnvironmentPostProcessor load property-source file(s) implicitly

package my.project;

public class MyEnvironmentPostProcessor extends AbstractConventionEnvironmentPostProcessor {

    @Override
    protected String getName(ConfigurableEnvironment environment, SpringApplication application) {
        return "property-source";
    }

    @Override
    protected String[] getLocationsPrefix(ConfigurableEnvironment environment, SpringApplication application) {
        return new String[]{
                "classpath:my-project"
        };
    }
}

DO NOT forget to register it. In your classpath:/META-INF/spring.factories:

org.springframework.boot.env.EnvironmentPostProcessor=my.project.MyEnvironmentPostProcessor

Now, file(s) will be loaded if exists.

  • classpath:my-project.conf
  • classpath:my-project.toml
  • classpath:my-project.yml
  • classpath:my-project.yaml
  • classpath:my-project.properties
  • classpath:my-project.xml (also properties)

Also, file(s) will be loaded if exists and myprofile profile is active.

  • classpath:my-project-myprofile.conf
  • classpath:my-project-myprofile.toml
  • classpath:my-project-myprofile.yml
  • classpath:my-project-myprofile.yaml
  • classpath:my-project-myprofile.properties
  • classpath:my-project-myprofile.xml (also properties)

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Authors

License

Apache 2.0 license. See LICENSE

About

spring boot starter for yaml/toml/hocon property-source

License:Apache License 2.0


Languages

Language:Makefile 100.0%