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 < ...
随机推荐
- ExtJs grid单选,多选
一. selType : 'checkboxmodel',singleSelect : true, // 单选multiSelect : true, // 多选singleSelects:['edit ...
- 粗结MySql数据库基础知识点之一
首先弄什么是数据库? 数据库就是用来存储和管理数据的仓库. 数据库存储数据的优点: 1.可存储大量的数据 2.方便检索 3.保持数据的一致性,完整性 4.安全 可共享 5.通过组合分析,可以产 ...
- C# SuperSocket服务端入门(一)
1,新建一个控制台应用程序,.NET版本4.0 2,添加SuperSocket(1.6.1).Binaries\Net40\Debug 目录下的: SuperSocket的dll文件( log4ne ...
- hdu 1695(莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Pet(dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=4707 题意:判断距离大于D的点有多少个. 思路: 邻接表建图,dfs每一个点,记录步数. #include &l ...
- bzoj1705[Usaco2007 Nov]Telephone Wire 架设电话线(dp优化)
1705: [Usaco2007 Nov]Telephone Wire 架设电话线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 441 Solved: ...
- doctype声明 过渡transitional 严格strict 框架frameset
DOCTYPE是document type(文档类型)的简写,用来说明你用的XHTML或者HTML是什么版本. 其中的DTD(例如上例中的xhtml1-transitional.dtd)叫文档类型定义 ...
- struts2OGNL表达式(三)
OGNL表达式 OGNL对象试图导航语言.${user.addr.name}这种写法就叫对象试图导航.Struts框架使用OGNL作为默认的表达式语言 OGNL不仅仅可以试图导航,支持比EL表达式更加 ...
- jsp页面导入excel文件的步骤及配置
上传使用flash插件 需要jquery.uploadify.min.js,uploadify.css,poi-ooxml-3.8-20120326.jar等 jsp页面: <%@include ...
- python--7、面向对象
什么是面向对象 对象,即抽象的一类事物中的某个具体的个体.这个世界中存在的一切皆为对象,不存在的也能创建出来. 较之面向过程的区别: 编程的复杂度远高于面向过程,不了解面向对象而立即上手基于它设计程序 ...