springboot获取默认配置文件中的配置项

Dear 丶 2022-11-10 14:14 385阅读 0赞

springboot默认的配置文件是application.properties。如何获取里面的配置项的值呢。

默认配置路径是在classpath根目录【resources】下,或者在classpath:/config目录下

application.properties配置文件中有一下两个配置:

  1. local.ip=192.168.1.100
  2. local.port=8088

方式一:在启动类里获取

  1. @SpringBootApplication
  2. public class MylslApplication {
  3. public static void main(String[] args) {
  4. ConfigurableApplicationContext context = SpringApplication.run(MylslApplication.class, args);
  5. System.err.println("local.ip=" + context.getEnvironment().getProperty("local.ip"));
  6. }
  7. }

输出:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70

方式二:在类中利用Environment获取

  1. 新建一个类如下:
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.core.env.Environment;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class MyConfig {
  7. @Autowired
  8. private Environment env;
  9. public void show(){
  10. System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
  11. }
  12. }

在启动类里调用下该show()方法,看下打印结果

  1. package com.lsl.mylsl;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. @SpringBootApplication
  6. public class MylslApplication {
  7. public static void main(String[] args) {
  8. ConfigurableApplicationContext context = SpringApplication.run(MylslApplication.class, args);
  9. System.err.println("local.ip=" + context.getEnvironment().getProperty("local.ip"));
  10. context.getBean(MyConfig.class).show();
  11. }
  12. }

输出结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 1

方式三:在类中利用@Value注解获取,直接赋值给属性

  1. package com.lsl.mylsl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.core.env.Environment;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class MyConfig {
  8. @Autowired
  9. private Environment env;
  10. @Value("${local.port}")
  11. private String localPort;
  12. public void show(){
  13. System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
  14. System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
  15. }
  16. }

结果输出:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 2

另外,配置文件支持引用配置

  1. local.ip=192.168.1.100
  2. local.port=8088
  3. name=springboot
  4. app.name=this name ${name}
  1. package com.lsl.mylsl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.core.env.Environment;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class MyConfig {
  8. @Autowired
  9. private Environment env;
  10. @Value("${local.port}")
  11. private String localPort;
  12. public void show(){
  13. System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
  14. System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
  15. System.err.println("MyConfig---name=" + env.getProperty("name"));
  16. System.err.println("MyConfig---app.name=" + env.getProperty("app.name"));
  17. }
  18. }

输出结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 3

默认配置文件及路径的重新指定

配置文件的名字和路径也是可以修改的,通过命令来重新指定

修改文件名字:—spring.config.name=app.properties

修改路径:—spring.config.location=classpath:/conf/app.properties;file:e:/temp/app.properties

修改路径可以是系统文件的,利用file:/来修改。

另外也可以通过编程的方式指定配置文件

比如在classpath:/conf下有个配置文件app.properties

  1. myusername=rootroot
  2. password=123456

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 4

写一个配置类,利用@PropertySource注解引入配置文件

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 5

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 6

如果需要指定多个配置文件,可以在PropertiesConfig加多个@PropertySource注解

也可以利用@PropertySources注解一次指定多个配置文件

@PropertySources({@PropertySource(“classpath:/conf/app.properties”),@PropertySource(“file:/e:/temp/jdbc.properties”)})

方式四:利用注解@ConfigurationProperties(prefix = “ds”)获取带前缀的配置项

  1. myusername=rootroot
  2. password=123456
  3. ds.url=http://192.168.1.200:8080/index.html
  4. ds.name=LSL
  1. package com.lsl.mylsl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.context.properties.ConfigurationProperties;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. @ConfigurationProperties(prefix = "ds")
  9. public class MyConfig {
  10. @Autowired
  11. private Environment env;
  12. @Value("${local.port}")
  13. private String localPort;
  14. @Value("${myusername}")
  15. private String myusername;
  16. @Value("${password}")
  17. private String password;
  18. private String url;
  19. private String name;
  20. public void show(){
  21. System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
  22. System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
  23. System.err.println("MyConfig---name=" + env.getProperty("name"));
  24. System.err.println("MyConfig---app.name=" + env.getProperty("app.name"));
  25. System.err.println("MyConfig---myusername=" + env.getProperty("myusername"));
  26. System.err.println("MyConfig---password=" + env.getProperty("password"));
  27. System.err.println("MyConfig---name=" + name + ",url="+url);
  28. }
  29. public String getUrl() {
  30. return url;
  31. }
  32. public void setUrl(String url) {
  33. this.url = url;
  34. }
  35. public String getName() {
  36. return name;
  37. }
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41. }

输出结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 7

对于带前缀的配置项,当然也可以通过上面的方式获取,也可以采用第四种方式获取,属性名必须后配置项后缀名字一致,且注解@ConfigurationProperties(prefix = “ds”)配置好前缀,且配置类里有setter\getter方法,这样才可以。

最后说下,如果获取配置文件中配置项集合

  1. yusername=rootroot
  2. password=123456
  3. ds.url=http://192.168.1.200:8080/index.html
  4. ds.name=LSL
  5. ds.ports[0]=9000
  6. ds.ports[1]=9001
  7. ds.ports[2]=9002
  1. package com.lsl.mylsl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.context.properties.ConfigurationProperties;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.stereotype.Component;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @Component
  10. @ConfigurationProperties(prefix = "ds")
  11. public class MyConfig {
  12. @Autowired
  13. private Environment env;
  14. @Value("${local.port}")
  15. private String localPort;
  16. @Value("${myusername}")
  17. private String myusername;
  18. @Value("${password}")
  19. private String password;
  20. private String url;
  21. private String name;
  22. //注入list
  23. private List<String> ports = new ArrayList<>();
  24. public void show(){
  25. System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
  26. System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
  27. System.err.println("MyConfig---name=" + env.getProperty("name"));
  28. System.err.println("MyConfig---app.name=" + env.getProperty("app.name"));
  29. System.err.println("MyConfig---myusername=" + env.getProperty("myusername"));
  30. System.err.println("MyConfig---password=" + env.getProperty("password"));
  31. System.err.println("MyConfig---name=" + name + ",url="+url);
  32. System.err.println("MyConfig---ports=" + ports );
  33. }
  34. public List<String> getPorts() {
  35. return ports;
  36. }
  37. public void setPorts(List<String> ports) {
  38. this.ports = ports;
  39. }
  40. public String getUrl() {
  41. return url;
  42. }
  43. public void setUrl(String url) {
  44. this.url = url;
  45. }
  46. public String getName() {
  47. return name;
  48. }
  49. public void setName(String name) {
  50. this.name = name;
  51. }
  52. }

输出结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Roa2xzbA_size_16_color_FFFFFF_t_70 8

发表评论

表情:
评论列表 (有 0 条评论,385人围观)

还没有评论,来说两句吧...

相关阅读