2.SpringBoot的properties的属性配置详解
SpringBoot是为了简化Spring应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,
我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程
SpringBoot虽然干掉了 XML 但未做到零配置,它体现出了一种约定优于配置,也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,
获得简单的好处,而又不失灵活性。一般情况下默认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配置、多环境配置、
外部命令引导等一系列功能。不用担心,这些SpringBoot都替我们考虑好了,我们只需要遵循它的规则配置即可.
一.准备前提
为了让SpringBoot更好的生成数据,我们需要添加如下依赖(该依赖可以不添加,但是在 IDEA 和 STS 中不会有属性提示,没有提示的配置就跟你用记事本写代码一样苦逼,出个问题弄哭你去),该依赖只会在编译时调用,所以不用担心会对生产造成影响…
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
二.使用系统的application.properties属性文件进行相关配置和值的注入
在application.properties写入如下配置内容
stu1.age=25
stu1.name=Luis
其次定义StudentProperties.java文件,用来映射我们在application.properties中的内容,这样一来我们就可以通过操作对象的方式来获得配置文件的内容了
1.创建StudentProperties.java
package cn.kgc.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 注解Component: 标注传递数据的实体类
* 注解ConfigurationProperties:标注属性文件的,
* prefix前缀则是属性文件中属性的前缀,
* 因为一个属性文件中可能配置很多,可以通过前缀区分
*/
@Component
@ConfigurationProperties(prefix = "stu")
public class StudentProperties {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentProperties{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
2.定义controller类来给StudentProperties类注入值
定义我们的PropertiesController用来注入StudentProperties测试我们编写的代码,值得注意的是Spring4.x以后,推荐使用构造函数的形式注入属性…
package cn.kgc.controller; import cn.kgc.properties.StudentProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2018/10/16.
*/
@RequestMapping("/properties")
@RestController
public class PropertiesController {
//对本类做日志记录
private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);
//创建接受属性文件的值的实体类
private final StudentProperties studentProperties;
@Autowired
public PropertiesController(StudentProperties studentProperties) {
this.studentProperties = studentProperties;
}
@GetMapping("/stuProperties")
public StudentProperties studentProperties() {
log.info("=================================================================================================");
log.info(studentProperties.toString());
log.info("=================================================================================================");
return studentProperties;
}
}
3.运行开启springBoot,在浏览器输入:http://localhost:9090/springboot1/properties/stuProperties ,可以在控制台和浏览器看到我们的数据


三、使用自定义的属性配置文件,进行值的相关注入
1. 定义一个名为teacher.properties的资源文件,自定义配置文件的命名不强制application开头

2.定义实体类用来接受springboot将将属性文件注入值
其次定义TeacherProperties.java文件,用来映射我们在teacher.properties中的内容。
package cn.kgc.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2018/10/16.
*/
@Component
@PropertySource("classpath:teacher.properties")
@ConfigurationProperties(prefix = "teacher")
public class TeacherProperties {
private int tid;
private String tname;
private String qq;
private String phone; public TeacherProperties() {
} public TeacherProperties(int tid, String tname, String qq, String phone) {
this.tid = tid;
this.tname = tname;
this.qq = qq;
this.phone = phone;
} public int getTid() {
return tid;
} public void setTid(int tid) {
this.tid = tid;
} public String getTname() {
return tname;
} public void setTname(String tname) {
this.tname = tname;
} public String getQq() {
return qq;
} public void setQq(String qq) {
this.qq = qq;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} @Override
public String toString() {
return "TeacherProperties{" +
"tid=" + tid +
", tname='" + tname + '\'' +
", qq='" + qq + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
3.在PropertiesController用来注入TeacherProperties测试我们编写的代码
package cn.kgc.controller; import cn.kgc.properties.StudentProperties;
import cn.kgc.properties.TeacherProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2018/10/16.
*/
@RequestMapping("/properties")
@RestController
public class PropertiesController { //对本类做日志记录
private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);
//创建接受属性文件的值的实体类
private final StudentProperties studentProperties;
//创建接受属性文件的值的实体类
private final TeacherProperties teacherProperties;
@Autowired
public PropertiesController(TeacherProperties teacherProperties, StudentProperties studentProperties) {
this.studentProperties = studentProperties;
this.teacherProperties = teacherProperties;
}
@GetMapping("/tecProperties")
public TeacherProperties teacherProperties() {
log.info("=================================================================================================");
log.info(teacherProperties.toString());
log.info("=================================================================================================");
return teacherProperties;
}
//--- @GetMapping("/stuProperties")
public StudentProperties studentProperties() {
log.info("=================================================================================================");
log.info(studentProperties.toString());
log.info("=================================================================================================");
return studentProperties;
}
//
}
4.先启动springBoot

5.在地址栏输入地址:http://localhost:9090/springboot1/properties/tecProperties查看结果


2.SpringBoot的properties的属性配置详解的更多相关文章
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- spring sessionFactory 属性配置详解,applicationContext中各种属性详解
1.Bean的id为sessionFactory,对应的类为AnnotationSessionFactory,即采用注解的形式实现hibernate. 2.hibernateProperties,配置 ...
- Spring容器的属性配置详解的六个专题
在spring IOC容器的配置文件applicationContext.xml里,有一些配置细节值得一提.我们将一些问题归结为以下几个专题. 专题一:字面值问题 配置的bean节点中的值,我们提 ...
- 从Spring到SpringBoot构建WEB MVC核心配置详解
目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...
- log4j.properties错误及配置详解
当在Eclipse上运行MapReduce程序遇到以上问题时,请检查项目中是否有log4j.properties配置文件,或者配置文件是否正确. 刚接触Hadoop的时候不太了解log4j.prope ...
- vSphere vSwitch网络属性配置详解
1.安全 混杂模式:把vSwitch当成是一个hub,同一台交换机上面所有的虚拟机都能接受到二层数据包. MAC地址更改:当vSwitch上面连接的某一个虚拟机MAC地址发生更改时,vSwitch是否 ...
- springboot配置详解
springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...
- SpringBoot—整合log4j2入门和log4j2.xml配置详解
关注微信公众号:CodingTechWork,一起学习进步. 引言 对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...
- log4j.properties配置详解
1.Loggers Loggers组件在此系统中被分为五个级别:DEBUG.INFO.WARN.ERROR和FATAL.这五个级别是有顺序的,DEBUG < INFO < WARN < ...
随机推荐
- 十分钟学会canvas
一句话描述:canvas是HTML5加入的用来绘制2D图像与文字的元素. 基础 简单步骤: var c = document.getElementById("mycanvas"); ...
- ios单元測试之GHUnit
1.相同创建一个測试的project, 2.通过cocoaPod来下载GHUnit框架,或者到github上下载.由于这个框架是开源的第三方框架. 同一时候加入QuartCore.framework( ...
- Android Fragment和Activity的交互介绍
Fragment和Activity的交互 一个Fragment的实例总是和包括它的Activity直接相关. fragment能够通过getActivity() 方法来获得Activity的实例.然后 ...
- 电脑升级win10后visio的问题
上周由于电脑意外蓝屏,系统从win7升级到了win10,昨天工作写文档时才发现缺少画图的工具,于是按照了visio2013,在编辑设计图时发现,一旦用visio打开或编辑图后再到word里设计图的内容 ...
- 希尔shell排序——java实现
希尔排序是对插入排序的优化,将插入排序的交换步长由1增加到h. 希尔排序的思想是使数组中任意间隔为h的元素有序.步长调幅为h = 3*h + 1, 也就是1,4,13,40,121,364, 1003 ...
- etcd数据备份与恢复验证
一.单机 说明:执行etcd备份数据的恢复的机器必须和原先etcd所在机器一致 1.单机备份 etcdctl --endpoints="https://10.25.72.62:2379&qu ...
- nodejs windows环境安装
相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼.有关nodejs的相关资料网上已经铺天盖地.由于它的高并发特性,造就了其特殊的应用地位. 国内目前关注最高,维护 ...
- Python 31 TCP协议 、socket套接字
1.TCP协议 可靠传输,TCP数据包没有长度限制,理论上可以无限长,但是为了保证网络的效率,通常TCP数据包的长度不会超过IP数据包的长度,以确保单个TCP数据包不必再分割. (1)三次握手建链接( ...
- 题解报告:hdu 4764 Stone(巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 Problem Description Tang and Jiang are good frie ...
- CSS样式优先级和权重问题(部分)
内联样式: <div style="font-size: 12px;">姓名</div> 外部样式: <link rel="styleshe ...