前言:了解过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. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class WebController {
  7. @Value("${test.msg}")
  8. private String msg;
  9. @RequestMapping("/index1")
  10. public String index1(){
  11. return "方式一:"+msg;
  12. }
  13. }

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

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

  1. package Solin.controller;
  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.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class WebController {
  9. @Autowired
  10. private Environment env;
  11. @RequestMapping("/index2")
  12. public String index2(){
  13. return "方式二:"+env.getProperty("test.msg");
  14. }
  15. }

注意:这种方式是依赖注入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核心配置文件,也是用上面两种方式读取其内容。例如:如下图使用方式一来读取:

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

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

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

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

创建管理配置的实体类:

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

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

创建测试Controller

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

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

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

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

创建管理配置的实体类:

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

转载 springboot 配置读取的更多相关文章

  1. Java后端精选技术:SpringBoot配置读取

    在早前的博客中曾经写过 Spring 程序通过 Bean 映射实现配置信息的读取. 在SpringBoot 框架中读取配置的方式变得非常多样,这导致读者在搜寻资料时反而容易迷糊. 到底,SpringB ...

  2. springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法

    springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...

  3. SpringBoot配置属性转载地址

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  4. SpringBoot基础系列-SpringBoot配置

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...

  5. springboot配置详解

    springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...

  6. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  7. SpringBoot配置(1) 配置文件application&yml

    SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...

  8. SpringBoot配置属性之Migration

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  9. spring boot 学习(十)SpringBoot配置发送Email

    SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...

随机推荐

  1. storm项目优化

    实现监控脚本监控topology运行状态

  2. 使用promisify来流程化异步操作

    现代js包括nodejs中有很多函数都是异步执行的, 我们总是需要写一个回调函数并且作为最后以一个参数传入,而我希望的是能像写promise这样的回调 promise .then() .then() ...

  3. B站上的一个MATLAB与神经网络的视频,捡漏

    ▶ av15514817.这里集中了一些从视频中学到的散点. ▶ 语句 "edit + 函数名" 可以打开部分内置函数的源代码.非公开的源代码这会打开一个全是注释的文档. ▶ 函数 ...

  4. zabbix 主动模式监控

    参考网站:  http://www.mamicode.com/info-detail-1724685.html http://www.cnblogs.com/dadonggg/p/8609674.ht ...

  5. Alpha版本检测报告

    1.Alpha版本测试报告 发布一篇随笔,作为项目的测试报告,内容包括: (1)测试计划 测试人员 工作安排 覃一霸 编写测试报告 张江波 执行测试.截图测试结果 测试功能 功能 描述 效果 结果 登 ...

  6. [ilink32 Error] Error: Unresolved external 'SendARP'

    [ilink32 Error] Error: Unresolved external 'SendARP' referenced from E:\APPOBJ\KSRGETMAC.OBJ #pragma ...

  7. delphi 泛型 c++builder 泛型

    delphi 泛型 System.Generics.Collections.pas TList<T> http://docwiki.embarcadero.com/Libraries/Be ...

  8. maven错误

    maven-enforcer-plugin (goal "enforce") is ignored by m2e. Plugin execution not covered by ...

  9. 可视化库-seaborn-回归分析绘图(第五天)

    1. sns.regplot() 和 sns.lmplot() 绘制回归曲线 import numpy as np import pandas as pd from scipy import stat ...

  10. fb 发布桌面应用图标

    1.以src文件夹为根目录,即图标放在src文件内 2.修改app.xml文件icon,按尺寸填入,如下图 同时可修改app应用的名字,接受中文,如下图红色涂鸦: 导出发行版的时候,注意打包内容有么有 ...