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. 详略。。设计模式1——单例。。。。studying

    设计模式1--单例 解决:保证了一个类在内存中仅仅能有一个对象. 怎么做才干保证这个对象是唯一的呢? 思路: 1.假设其它程序可以任意用new创建该类对象,那么就无法控制个数.因此,不让其它程序用ne ...

  2. Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)

    接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...

  3. android自带的处理Bitmap out Memory 的处理,我仅仅是改变了些写法成为自己用的东西

    每天上万次的启动载入证明这个载入是不错的; 第三方的载入框架非常多,推荐使用成熟框架,比方大家都知道的ImageLoad等, 这里的仅仅供学习; 我也曾在一个菜谱的项目里写过载入手机相冊图片的,只是当 ...

  4. HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))

    Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. WCF学习笔记——配置服务引用

    WCF传过来的东西要序列化. 比如,在WCF服务中,象这么个方法 public IEnumerable<UserItem> GetUserList() 默认情况下,在客户端会调用,是这样: ...

  6. SqlServer还原步骤

    SqlServer还原步骤 2009-09-05 10:32:12|  分类: 数据库|字号 订阅     1 . 删除原有数据库 新建数据库  hywlxt 2. 在master 中新建存储过程 k ...

  7. spring:利用Spring AOP 使日志输入与方法分离

    对方法进行日志输出是一种很常见的功能.传统的做法是把输出语句写在方法体的内部,在调用该方法时,用输入语句输出信息来记录方法的执行! 1.先写一个普通类: package com.importnew; ...

  8. 杂项-TMod:常见错误

    ylbtech-杂项-TMod:常见错误 1.返回顶部 1. 1.1. {Template Error} TypeError: dateDiff is not a function at Array. ...

  9. hihoCoder 1187

    今天BC爆0了....但是日子还是要过的....要回学校毕业了~~大学就这么“荒废”了. 这个是hihoCoder的1187,比较基础的一道题. 题目链接: http://hihocoder.com/ ...

  10. 《疯狂Python讲义》重要笔记——Python简介

    简介 Python是一种面向对象.解释型.弱类型的脚本语言,同时也是一种功能强大的通过语言,它提供了高效的高级数据结构,还有简单有效的面向对象编程. 在大数据.人工智能(AI)领域应用广泛,因此变得流 ...