一、新建maven工程:springboot-configfile-demo,完整工程如下:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>springboot.configfile.demo</groupId>
<artifactId>springboot-configfile-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

二、新建三个配置文件

1、主配置文件application.yml(程序默认从该文件读取配置信息,放在该配置文件的配置,可以直接在代码通过@Value读取)

server:
port: 8083
context-path: / teacher:
name: boshen
password: 123456

2、自定义配置文件1,configs/test1.properties(没有前缀)

doctor_name=zhangsan
doctor_age=35

3、自定义配置文件2,configs/test2.properties(有前缀,后面通过一个Student类来接收信息)

student.name=lishi
student.sex=man student.bookList[0]=english
student.bookList[1]=history student.scoreMap[english]=95
student.scoreMap[history]=98

三、新建类Student

package configfile.demo.configBean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import java.io.Serializable;
import java.util.List;
import java.util.Map; /**
* @author Administrator
* @date 2019/02/15
*/
@Configuration
@ConfigurationProperties(prefix = "student")
@PropertySource("classpath:configs/test2.properties"
)
@Component
public class Student implements Serializable{
private String name;
private String sex; private List<String> bookList; private Map<String,String> scoreMap; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public List<String> getBookList() {
return bookList;
} public void setBookList(List<String> bookList) {
this.bookList = bookList;
} public Map<String, String> getScoreMap() {
return scoreMap;
} public void setScoreMap(Map<String, String> scoreMap) {
this.scoreMap = scoreMap;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", bookList=" + bookList +
", scoreMap=" + scoreMap +
'}';
}
}

四、新建controller类,进行测试

package configfile.demo.controller;

import configfile.demo.configBean.Student;
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; /**
* @author Administrator
* @date 2019/02/15
*/
@RestController
@RequestMapping("/config-test")
public class TestController { @Value("${teacher.name}")
private String teacherName;
@Value("${teacher.password}")
private String teacherPassword; @Value("${doctor_name}")
private String doctorName;
@Value("${doctor_age}")
private Integer doctorAge; @Autowired
private Student student; @RequestMapping(value = "/read",method = RequestMethod.GET)
public void postTest(){
System.out.println("以下是默认的application.yml的配置:==========");
System.out.println("teacher.name:"+teacherName);
System.out.println("teacher.password:"+teacherPassword); System.out.println("以下是configs/test1.properties的配置:==========");
System.out.println("doctor_namee:"+doctorName);
System.out.println("doctor_age:"+doctorAge); System.out.println("以下是configs/test2.properties的配置:==========");
System.out.println(student.toString());
}
}

五、新建springboot启动类

package configfile.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; /**
* @author Administrator
* @date 2019/02/15
*/
@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(value = "classpath:/configs/test1.properties", ignoreResourceNotFound = true)
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}

六,启动程序,在postman测试,并查看控制台输出

springboot读取properties和yml配置文件的更多相关文章

  1. springboot读取properties(yml)的几种常用方式

    boot项目中一些秘钥等不常变动的信息大多存储在配置文件中,那么我们怎么获取配置文件中的属性呢? 以获取server端口号为例讲解几种方法:配置信息如下 一:使用@Value注解 @Value(&qu ...

  2. SpringBoot使用注解(@value)读取properties(yml)文件中 配置信息

    为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性. 1. 两种使用方法1)@Value("#{co ...

  3. 使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案

    1.如果启动项目不加载application.yml配置文件,那么请确认下是否应用了Resources为项目资源文件夹 2.如果项目起初是可以正常使用的,突然不知道改了什么,然后进行启动项目的时候不加 ...

  4. Springboot .properties或.yml配置文件读取pom.xml文件值

    有时候配置文件需要读取pom文件配置<properties></properties>中间自定义属性值的时候可以用@@获取 例:@package.parameter@ 然后还需 ...

  5. Springboot读取properties配置文件数据

    一.使用@ConfigurationProperties来读取 1.Coffer entity @Configuration @ConfigurationProperties(prefix = &qu ...

  6. springboot动态读取properties 和yml的配置

    properties使用PropertiesLoaderUtils,yml使用YamlPropertySourceLoader application.properties microsoft.def ...

  7. SpringBoot 读取properties配置文件 @Value使用 中文乱码问题

    一,idea中配置文件中文乱码问题 使用idea开发,读取properites配置文件 配置: #app 菜单 #没有限制,所有人都可访问的菜单 menu.unlimited=订单审批,现场尽调,合作 ...

  8. springboot读取自己定义的配置文件的方式以及使用joda_time来处理时间日期

    总的来说呢,有两种方式,一种是原始的方式,即使用PropertiesUtils来读取配置文件. 第二种就是使用springboot的注解的方式来读取配置文件. 1.原始方式处理属性和时间日期: 工具类 ...

  9. spring读取properties和其他配置文件的几种方式

    1.因为spring容器的一些机制,在读取配置文件进行数据库的配置等等是很有必要的,所以我们要考虑配置文件的的读取方式以及各个方式的实用性 2.配置文件的读取方式我这里介绍2种,目的是掌握这2种就可以 ...

随机推荐

  1. 配置iis支持json解析,配置ssi

    配置json解析: 添加mime:*.json  类型 text/json 安装iis应用程序开发中的asp功能 添加处理程序映射: 添加脚本映射 请求路径:*.json 可执行文件:C:\Windo ...

  2. 回滚的意义---JDBC事务回滚探究

    JDBC手动事务提交回滚的常见写法一直是rollback写在commit的catch之后: try{ conn.setAutoCommit(false); ps.executeUpdate(); ps ...

  3. java多线程—Runnable、Thread、Callable区别

    多线程编程优点 进程之间不能共享内存,但线程之间共享内存非常容易. 系统创建线程所分配的资源相对创建进程而言,代价非常小. Java中实现多线程有3种方法: 继承Thread类 实现Runnable接 ...

  4. 事务、事务特性、事务隔离级别、spring事务传播特性

    事务.事务特性.事务隔离级别.spring事务传播特性   1.什么是事务: 事务是程序中一系列严密的操作,所有操作执行必须成功完成,否则在每个操作所做的更改将会被撤销,这也是事务的原子性(要么成功, ...

  5. java数据类型关系及关系

    java中有常见的基本数据类型和引用数据类型,基本数据类型为四类八种如下 整数型(byte,short,int,long) 浮点型(float,double) 字符型(char) 布尔型(boolea ...

  6. java利用泛型实现不同类型可变参数

    public class VP { public <T> void printMsg(T... args){ for (T t:args){ System.out.println(&quo ...

  7. 简单快捷使用Git

    1.简介和安装Git是世界上目前最先进的分布式版本控制系统.安装:https://git-for-windows.github.io下载.设置姓名和email:git config --global ...

  8. ElasicSearch(4) 与jest结合

    https://spring.io/projects/spring-data-elasticsearch https://docs.spring.io/spring-data/elasticsearc ...

  9. oracle数据链接

    using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; u ...

  10. C语言递归函数讲解

    递归函数是什么? 是函数.................... 你可以把它理解成是for循环与死循环的结合的函数.简单的说:递归函数是有条件终止的死循环函数: 死循环函数这里是指在函数体中调用自身: ...