转载 springboot 配置读取

摘要:
访问:http://localhost:8088/index1然后我们得到:“方法1:HelloWorldSpringBoot”方法2:使用环境方法packageSolin。控制器;importorg.springframework.beans.factory.annotation。自动连线;importorg.springframework.beans.factory.annotation。价值importorg.springframework.core.env。环境导入组织弹簧框架。web。ind。注释。请求映射;导入组织弹簧框架。web。ind。注释。RestController;@RestControllerpublicclassWebController{@AutowiredprivateEnvironmentenv;@RequestMappingpublicStringindex2(){return“Method 2:”+env.getProperty;}}注意:此方法由依赖注入Evnironment完成。将@Autowired注释添加到创建的成员变量privateEnvironmentenv以完成依赖注入,然后使用env。getProperty以读取相应的值。访问:http://localhost:8088/index2然后我们得到:“方法2:HelloWorldSpringBoot”核心配置文件application-dev。yml内容如下:#--**银行配置文件路径startczb:configPath:/usr/local/czbConfig/config。properties#--浙商银行相关配置文件路径endapplication*。yml核心配置文件,也使用上述两种方法读取其内容。

前言:了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息。

一.读取核心配置文件信息application.properties的内容

     核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

(1)核心配置文件application.properties内容如下:

  1. test.msg=Hello World SpringBoot  

(1)方式一:使用@Value方式(常用)

  1. package Solin.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. @RestController  
  8. public class WebController {  
  9.     @Value("${test.msg}")  
  10.     private String msg;  
  11.       
  12.     @RequestMapping("/index1")   
  13.     public String index1(){  
  14.         return "方式一:"+msg;  
  15.     }  
  16. }

注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:8088/index1时得到:"方式一:Hello World SpringBoot"

(2)方式二:使用Environment方式


  1. package Solin.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.annotation.Value;  
  5. import org.springframework.core.env.Environment;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. @RestController  
  10. public class WebController {  
  11.     @Autowired  
  12.     private Environment env;  
  13.       
  14.     @RequestMapping("/index2")   
  15.     public String index2(){  
  16.         return "方式二:"+env.getProperty("test.msg");  
  17.     }  
  18. }  

注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。

访问:http://localhost:8088/index2时得到:"方式二:Hello World SpringBoot"

(2)核心配置文件application-dev.yml内容如下:

 
  1. # --**银行配置文件路径 start
    czb:
      configPath: /usr/local/czbConfig/config.properties
    # --浙商银行相关配置文件路径 end

application*.yml核心配置文件,也是用上面两种方式读取其内容。例如:如下图使用方式一来读取:

转载 springboot 配置读取第1张

二.读取自定义配置文件信息,例如:author.properties

为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件authorA.properties

resources/author/authorA.properties内容如下:

 
  1. auth.name=Solin  
  2. auth.age=22  

创建管理配置的实体类:


  1. package Solin.controller;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.stereotype.Component;  
  6.     
  7. //加上注释@Component,可以直接其他地方使用@Autowired来创建其实例对象
  8. @Component  
  9. //springboot1.5之前的版本这么用,1.5版本之后取消了locations属性,替代方案看最下面。
  10. @ConfigurationProperties(prefix = "auth",locations = "classpath:/author/authorA.properties") 
  11. //在启动类上增加@EnableConfigurationProperties注解,激活自定义的配置类(重要)
  12. public class MyWebConfig{  
  13.     private String name;  
  14.     private int age;  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public int getAge() {  
  22.         return age;  
  23.     }  
  24.     public void setAge(int age) {  
  25.         this.age = age;  
  26.     }  
  27. }  

注意:
    在@ConfigurationProperties注释中有两个属性:
              locations:指定配置文件的所在位置
              prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以auth.开头)
    使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。

创建测试Controller

 
  1. package Solin.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.ResponseBody;  
  7.   
  8. @Controller    
  9. public class ConfigController {  
  10.     @Autowired  
  11.     private MyWebConfig conf;  
  12.       
  13.     @RequestMapping("/test")   
  14.     public @ResponseBody String test() {  
  15.         return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge();   
  16.     }  
  17. }  

注意:由于在Conf类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。

访问:http://localhost:8088/test时得到:"Name:Solin---Age:22"


备注:spring boot1.5以上版本@ConfigurationProperties取消locations注解后的替代方案,只是下面注解变了,别的都一样。

创建管理配置的实体类:


  1. package Solin.controller;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.stereotype.Component;  
  6.     
  7. @Component  
  8. @ConfigurationProperties(prefix ="auth") 
  9. @PropertySource("classpath:/author/authorA.properties")//@PropertySource来指定自定义的资源目录
  10. //在启动类上取消掉@EnableConfigurationProperties注解,springboot1.5版本之后不需要@EnableConfigurationProperties注解了。
  11. public class MyWebConfig{  
  12.     private String name;  
  13.     private int age;  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public int getAge() {  
  21.         return age;  
  22.     }  
  23.     public void setAge(int age) {  
  24.         this.age = age;  
  25.     }  
  26. }  
 
 
  1. test.msg=Hello World SpringBoot  

免责声明:文章转载自《转载 springboot 配置读取》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇tomcat8热部署配置--maven自动发布项目到tomcat8(如何支持远程访问部署)微星主板刷新BIOS指南下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

详解Intellij IDEA中.properties文件中文显示乱码问题的解决

首先,你可能会见到如下提示: File encoding is disabled because .properties file (see Settings|Editor|File Encodings|Properties Files) 具体如下图。没截全图,太大了,只截取了提示部分。 在这之前,我一直以为,我已经设置了我这个编辑器下的所有文件的编码格...

Eclipse的常见使用错误及编译错误

http://blog.csdn.net/zhandoushi1982/article/details/8049404 (1)导入工程时出现:Error generating final archive: Debug Certificate         这样的编译错误是由于新版的Android SDK调试key有1年的试用期限制,如果当年电脑的And...

application.properties文件配置

  在application.properties文件中配置mysql连接配置文件。 spring.datasource.url = jdbc:mysql://localhost:3306/DatebaseName(自己数据库名)spring.datasource.username = rootspring.datasource.password = 12...

springboot多环境下的自定义配置文件,并读取到常量

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Com...

SpringBoot---基本了解

什么是 Spring Boot?   Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。Spring Boot 的最大的优势是“约定优于配置“ SpringBoot约定优于配置的体现   一、Maven的目录结构...

yml在线格式转换工具(properties)

分享一个在线properties转yml工具,也支持yml转properties, 域名非常简单好记,直接在地址栏里输入toyaml.com, 地址:http://toyaml.com/ yml,即yaml文本格式文件的后缀名,yaml可以用来替代properties,可读性更好一些。 目前springboot的配置文件已经同时支持properties和y...