转载 springboot 配置读取
前言:了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息。
一.读取核心配置文件信息application.properties的内容
核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
(1)核心配置文件application.properties内容如下:
- test.msg=Hello World SpringBoot
(1)方式一:使用@Value方式(常用)
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class WebController {
- @Value("${test.msg}")
- private String msg;
- @RequestMapping("/index1")
- public String index1(){
- return "方式一:"+msg;
- }
- }
注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:8088/index1时得到:"方式一:Hello World SpringBoot"
(2)方式二:使用Environment方式
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.env.Environment;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class WebController {
- @Autowired
- private Environment env;
- @RequestMapping("/index2")
- public String index2(){
- return "方式二:"+env.getProperty("test.msg");
- }
- }
注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。
访问:http://localhost:8088/index2时得到:"方式二:Hello World SpringBoot"
(2)核心配置文件application-dev.yml内容如下:
- # --**银行配置文件路径 start
czb:
configPath: /usr/local/czbConfig/config.properties
# --浙商银行相关配置文件路径 end
application*.yml核心配置文件,也是用上面两种方式读取其内容。例如:如下图使用方式一来读取:
二.读取自定义配置文件信息,例如:author.properties
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件authorA.properties
resources/author/authorA.properties内容如下:
- auth.name=Solin
- auth.age=22
创建管理配置的实体类:
- package Solin.controller;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
- //加上注释@Component,可以直接其他地方使用@Autowired来创建其实例对象
- @Component
- //springboot1.5之前的版本这么用,1.5版本之后取消了locations属性,替代方案看最下面。
- @ConfigurationProperties(prefix = "auth",locations = "classpath:/author/authorA.properties")
- //在启动类上增加@EnableConfigurationProperties注解,激活自定义的配置类(重要)
- public class MyWebConfig{
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
注意:
在@ConfigurationProperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以auth.开头)
使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。
创建测试Controller
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- public class ConfigController {
- @Autowired
- private MyWebConfig conf;
- @RequestMapping("/test")
- public @ResponseBody String test() {
- return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge();
- }
- }
注意:由于在Conf类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。
访问:http://localhost:8088/test时得到:"Name:Solin---Age:22"
备注:spring boot1.5以上版本@ConfigurationProperties取消locations注解后的替代方案,只是下面注解变了,别的都一样。
创建管理配置的实体类:
- package Solin.controller;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
- @Component
- @ConfigurationProperties(prefix ="auth")
- @PropertySource("classpath:/author/authorA.properties")//
@PropertySource来指定自定义的资源目录 - //在启动类上取消掉@EnableConfigurationProperties注解,springboot1.5版本之后不需要@EnableConfigurationProperties注解了。
- public class MyWebConfig{
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- test.msg=Hello World SpringBoot
转载 springboot 配置读取的更多相关文章
- Java后端精选技术:SpringBoot配置读取
在早前的博客中曾经写过 Spring 程序通过 Bean 映射实现配置信息的读取. 在SpringBoot 框架中读取配置的方式变得非常多样,这导致读者在搜寻资料时反而容易迷糊. 到底,SpringB ...
- springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法
springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...
- SpringBoot配置属性转载地址
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
- SpringBoot基础系列-SpringBoot配置
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...
- springboot配置详解
springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- SpringBoot配置(1) 配置文件application&yml
SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...
- SpringBoot配置属性之Migration
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
- spring boot 学习(十)SpringBoot配置发送Email
SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...
随机推荐
- 超全整理!Linux性能分析工具汇总合集
转自:http://rdc.hundsun.com/portal/article/731.html?ref=myread 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章. ...
- (c#) 销毁资源和释放内存
0. 什么是资源? .NET 框架中如何访问资源? 所谓的资源就是程序中可利用的数据,譬如:字符串.图片和任何二进制数据,包括任何类型的文件. 在面向对象的环境中,每一个类型都标识为某些程序所用的资源 ...
- LINUX ifconfig 命令详解
ifconfig 配置和显示Linux系统网卡的网络参数 补充说明 ifconfig命令 被用于配置和显示Linux内核中网络接口的网络参数.用ifconfig命令配置的网卡信息,在网卡重启后机器重启 ...
- hadoop分布式快速搭建
hadoop分布式快速搭建 1.配置主节点与从节点的ssh互信:[其中在主从节点主机的/etc/hosts文件中需绑定主机名ip间的映射关系; 如,192.168.1.113 node0 192.16 ...
- 17.scrapy框架简例使用
目标:创建scrapy项目 创建一个spider来抓取站点和处理数据 通过命令行将抓取内容导出 1.创建项目 scrapy startproject tutorial 2.创建spider cd tu ...
- spring security 配置xml 参考
https://blog.csdn.net/zsq520520/article/details/77880491
- angular 参考文档
https://www.w3schools.com/angular/ 参考二: https://www.angular.cn/guide/reactive-forms
- 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)
转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...
- 使用.htaccess文件
禁止对无索引文件的目录进行文件列表展示 默认情况下,当我们访问网站的某个无索引文件(如index.html,index.htm或 index.php)目录时,服务器会显示该目录的文件和子目录列表,这是 ...
- cmd 查看本地端口被占用程序
netstat -ano 列出当前活动的网络连接 参数:-a所有侦听端口 -n以数字格式显示地址和端口号 -o显示进程号 tasklist|find "8888" 找出占用888 ...