- External setting
Spring Download allows you to externalize your configuration so that you can work with the same application code in different environments. You can use property files, YAML files, environment variables, and command line arguments to externalize the configuration. Property values ββcan be entered directly into your beans using the @Value annotation, accessed via abstraction of Spring s environment, or attached to structured objects via @ConfigurationProperties.
Spring Boot uses a very specific PropertySource order, which is designed to intelligently override values. Properties are considered in the following order:
- General properties of Devtools settings in your home directory (~ / .spring-boot-devtools.properties when devtools is active).
- @TestPropertySource annotations for your tests.
- @ SpringBootTest # attribute attribute attribute for your tests.
- Command line arguments.
- Properties from SPRING_APPLICATION_JSON (inline JSON, inline in an environment variable or system property)
- ServletConfig initialization options.
- Parameters init ServletContext.
- JNDI attributes from java: comp / env.
- Java System Properties (System.getProperties ()).
- OS environment variables.
- A RandomValuePropertySource object that only has properties in random order. *.
- Profile-dependent application properties outside your packaged jar (application- {profile} .properties and YAML variants)
- Profile-dependent application properties packaged inside your jar (application- {profile} .properties and YAML variants)
- Application properties outside your packaged jar (application.properties and YAML options).
- Application properties packaged in your jar (application.properties and YAML options).
- @PropertySource for your @Configuration classes.
- Default properties (set using SpringApplication.setDefaultProperties).
The program below prints properties from the spring boot environment.
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ApplicationObjectSupport; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.stereotype.Component; import org.springframework.web.context.support.StandardServletEnvironment; @Component public class EnvironmentLogger extends ApplicationObjectSupport { @Override protected void initApplicationContext(ApplicationContext context) throws BeansException { Environment environment = context.getEnvironment(); String[] profiles = environment.getActiveProfiles(); if(profiles != null && profiles.length > 0) { for (String profile : profiles) { System.out.print(profile); } } else { System.out.println("Setting default profile"); }
Sudhakar
source share