在这篇文章中,主要是清楚两种基本的读取配置文件中的值的方式。然后,在最后,介绍多配置文件的使用方式。

  在加载完配置后,对象可以使用引用的方式使用。例如案例三。

一:url的配置

1.配置

  默认配置文件是application.properties

  

2.配置

  配置端口

  配置context path

  

3.启动效果

  

4.第二种配置方式

  要先删除application.properties文件,保留一个即可。

  

  

5.启动效果

  

二:字段的配置

1.配置文件

  

2.案例一,Java文件【直接在controller中使用】

 package com.caojun.springboot;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloSpringBoot { @Value("${cupsize}")
private String cupSize; @Value("${age}")
private Integer age; @RequestMapping(value="/hello")
public String say(){
return("cupSize="+cupSize+",age="+age);
}
}

3.启动

  

4.案例二,配置中使用配置【知道即可】

  

5.Java文件

package com.caojun.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloSpringBoot { @Value("${content}")
private String content; @RequestMapping(value="/hello")
public String say(){
return(content);
}
}

6.启动效果

  

7.案例三,简化使用字段的方法,配置文件

  

8.新建Java类

  需要使用Component进行加载bean

  需要使用ConfigurationProperties进行对属性进行配置。

 package com.caojun.springboot;

 import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "people")
public class PeoplePerties {
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

9.Java程序

 package com.caojun.springboot;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloSpringBoot { @Autowired
private PeoplePerties peoplePerties; @RequestMapping(value="/hello")
public String say(){
return peoplePerties.getName()+"====="+peoplePerties.getAge();
}
}

10.启动运行

  

三:多配置文件

1.多配置文件

  在默认的配置文件中,指定将要使用的配置文件,则会在启动的时候,读取两个配置文件中的配置。

  先复制application,然后分别命名:

    

  修改application.yml:

    

  dev文件

    

2.运行

  

SpringBoot属性配置的更多相关文章

  1. springBoot属性配置和使用

    Spring Boot 属性配置和使用 1.添加属性文件 application.properties (名字固定) 2.访问端口生效 3.更多配置参考 # ===================== ...

  2. SpringBoot属性配置-第三章

    1.application.yml配置#自定义参数对象book: name: A id: 1 page: 100 2.创建实体类: /** * @Auther: youqc * @Date: 2018 ...

  3. SpringBoot总结之属性配置

    一.SpringBoot简介 SpringBoot是spring团队提供的全新框架,主要目的是抛弃传统Spring应用繁琐的配置,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...

  4. SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、属性配置、多环境配置

    1 InteliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到官 ...

  5. [02] SpringBoot的项目属性配置

    1.application.properties 简述 配置文件的使用和调整都非常方便,直接在项目默认的classpath下的application.properties文件中做调整即可.例如Spri ...

  6. springboot快速入门(二)——项目属性配置(日志详解)

    一.概述 application.properties就是springboot的属性配置文件 在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring ...

  7. spring-boot 属性定义和配置bean

    自定义bean属性 1.定义bean属性 // 通过@ConfigurationProperties加载properties文件内的配置, // 通过prefix属性指定properties的配置的前 ...

  8. SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、yml属性配置、多环境配置、自定义properties配置

    1 IntelliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到 ...

  9. SpringBoot项目属性配置-第二章

    SpringBoot入门 1. 相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了 ...

随机推荐

  1. 算法学习——从bzoj2286开始的虚树学习生活

    [原创]转载请标明原作者~ http://www.cnblogs.com/Acheing/ 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2 ...

  2. 【转】Example of using the --info linker option

    5.3 Example of using the --info linker option This is an example of the output generated by the --in ...

  3. hdu5583

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...

  4. javaWeb接口开发

    链接:http://blog.csdn.net/zxw136511485/article/details/51437115

  5. Excel:一维表和二维表 互转

    一.一维表转二维表 数据源: 一份流水账式的值班表,为了便于打印张贴,现在需要使其变成这样的样式: 也就是从一维表变成传说中的二维表. 1.新建查询 依次单击[数据]→[新建查询] →[从文件]→[从 ...

  6. 访问修饰符---java基础总结

  7. python---django中文件上传

    服务端: def upload(req): if req.method == "GET": return render(req, 'upload.html') else: prin ...

  8. 支付宝APP支付,提示代码 ALIN10070

    ALIN10070 此代码时ALI64代码拆分后的细分代码: 代表签名验证失败等相关问题: 如果近期修改过或者续签 过签约协议,也需要更新公私钥.

  9. 字符串日期转化以及yyyy-MM-dd HH:mm:ss大小写解释

    字符串日期转化 字符串转换为Calendar对象: // 日期字符串 private String dateStr; // 将字符串转换后的Calender对象 private Calendar ca ...

  10. Java基础打包以及批处理命令运行

    1.前期准备