spring boot 获取配置信息的常用方式
spring boot应用程序获取配置文件的配置信息的方法:
1.通过@Value注释获取
2.通过Environment类获取
3.通过@ConfigurationProperties获取
application.properties文件内容:
1 server.port=8080 2 3 4 spring.main.banner-mode=off 5 6 myconfig.mail.port=1234
myconfig.properties文件内容:
1 myconfig.mail.address=localhost 2 myconfig.mail.userName=myuser
HelloController.java文件内容:
1 import javax.annotation.Resource; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.boot.context.properties.ConfigurationProperties; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.PropertySource; 8 import org.springframework.core.env.Environment; 9 import org.springframework.stereotype.Component; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RestController; 12 13 @RestController 14 public class HelloController { 15 @Resource 16 private Environment environment; 17 18 @Value("${server.port}") 19 private String port; 20 21 @Autowired 22 MyconfigProperties myconfig; 23 24 @RequestMapping("/hello") 25 public String Hello() { 26 System.out.println("server.port=" + environment.getProperty("server.port")); 27 System.out.println("server.port=" + port); 28 29 System.out.println("address=" + myconfig.getAddress() + ",userName=" + myconfig.getUserName() + ",port=" + myconfig.getPort()); 30 31 return "Hello world!"; 32 } 33 } 34 35 @Component 36 @PropertySource({"classpath:myconfig.properties","classpath:application.properties"}) 37 @ConfigurationProperties(prefix= "myconfig.mail") 38 class MyconfigProperties { 39 private String address; 40 private String userName; 41 private Integer port; 42 43 public Integer getPort() { 44 return port; 45 } 46 public void setPort(Integer port) { 47 this.port = port; 48 } 49 public String getAddress() { 50 return address; 51 } 52 public void setAddress(String address) { 53 this.address = address; 54 } 55 56 public String getUserName() { 57 return userName; 58 } 59 public void setUserName(String userName) { 60 this.userName = userName; 61 } 62 }
除这些方式以外,spring boot还有其他的配置信息方式:
https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/htmlsingle/#boot-features-external-config
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.propertieswhen devtools is active). @TestPropertySourceannotations on your tests.@SpringBootTest#propertiesannotation attribute on your tests.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON(inline JSON embedded in an environment variable or system property) ServletConfiginit parameters.ServletContextinit parameters.- JNDI attributes from
java:comp/env. - Java System properties (
System.getProperties()). - OS environment variables.
- A
RandomValuePropertySourcethat only has properties inrandom.*. - Profile-specific application properties outside of your packaged jar (
application-{profile}.propertiesand YAML variants) - Profile-specific application properties packaged inside your jar (
application-{profile}.propertiesand YAML variants) - Application properties outside of your packaged jar (
application.propertiesand YAML variants). - Application properties packaged inside your jar (
application.propertiesand YAML variants). @PropertySourceannotations on your@Configurationclasses.- Default properties (specified using
SpringApplication.setDefaultProperties).
浙公网安备 33010602011771号