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的属性配置详解的更多相关文章

  1. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  2. spring sessionFactory 属性配置详解,applicationContext中各种属性详解

    1.Bean的id为sessionFactory,对应的类为AnnotationSessionFactory,即采用注解的形式实现hibernate. 2.hibernateProperties,配置 ...

  3. Spring容器的属性配置详解的六个专题

    在spring IOC容器的配置文件applicationContext.xml里,有一些配置细节值得一提.我们将一些问题归结为以下几个专题.   专题一:字面值问题 配置的bean节点中的值,我们提 ...

  4. 从Spring到SpringBoot构建WEB MVC核心配置详解

    目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...

  5. log4j.properties错误及配置详解

    当在Eclipse上运行MapReduce程序遇到以上问题时,请检查项目中是否有log4j.properties配置文件,或者配置文件是否正确. 刚接触Hadoop的时候不太了解log4j.prope ...

  6. vSphere vSwitch网络属性配置详解

    1.安全 混杂模式:把vSwitch当成是一个hub,同一台交换机上面所有的虚拟机都能接受到二层数据包. MAC地址更改:当vSwitch上面连接的某一个虚拟机MAC地址发生更改时,vSwitch是否 ...

  7. springboot配置详解

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

  8. SpringBoot—整合log4j2入门和log4j2.xml配置详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...

  9. log4j.properties配置详解

    1.Loggers Loggers组件在此系统中被分为五个级别:DEBUG.INFO.WARN.ERROR和FATAL.这五个级别是有顺序的,DEBUG < INFO < WARN < ...

随机推荐

  1. &lt;转&gt;Openstack Ceilometer监控项扩展

    Openstack ceilometer主要用于监控虚拟机.服务(glance.image.network等)和事件.虚拟机的监控项主要包含CPU.磁盘.网络.instance.本文在现有监控项的基础 ...

  2. oc52--autorelease1

    // // main.m /* autorelease也是用于内存管理的,给对象发送autorelease消息就会把对象放入autoreleasepool这个池子中,当池子销毁的时候会对池子里面的所有 ...

  3. C# 读取ini文件 百度问问学习文档

    C# 读取ini文件 10 有多个section,现想读取整个ini文件和指定section下所有内容 补充: 发布答案可以,请对准题目啊,我不要指定节点的内容,我知道!我要的是读取指定区域的内容,假 ...

  4. System.IO.Path

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  5. SQL 导出表数据存储过程

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- ...

  6. iOS中的数据库—使用FMDB

    一.回顾 iOS中的数据存储方式 1.XML属性列表(plist) 写入OC的一些基本数据类型,不是所有对象都可以写入 2.Preference(偏好设置) 本质还是通过“plist”来存储数据,但是 ...

  7. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...

  8. mybatis+oracle实现一对多,多对一查询

    首先创建表 学生表 create table stu(       id number(11) primary key,       name varchar2(255),       age num ...

  9. Android开发利器之ActivityTracker

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/113 Android开发利器之ActivityTracke ...

  10. 题解报告:hdu 1848 Fibonacci again and again(尼姆博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...