springBoot读取配置文件
2019-05-30 17:52
134 查看
springBoot读取配置文件
pom.xml配置
在pom文件中我们加上配置文件加载器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
读取类配置
package com.hanlin.springboot.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author:hanlin.yuan * @date:2019/05/30 */ @Component @ConfigurationProperties(prefix = "student") public class Student { 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
注意@ConfigurationProperties其中的prefix是只的读取配置文件中的对象名称
application.yml配置文件
student: age: 18 name: 张三
注意:yml配置中,对象名称后面一定要有空格:列如:age:(空格)18
测试类
package com.hanlin.springboot; import com.hanlin.springboot.bean.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootDemo01ApplicationTests { @Autowired private Student student; @Test public void contextLoads() { System.out.println(student); } }
测试结果:
相关文章推荐
- 如何在SpringBoot下读取自定义properties配置文件
- springboot 读取配置文件的方式
- Springboot读取配置文件的两种方法
- SpringBoot 读取配置文件及profiles切换配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
- SpringBoot读取配置文件中文属性值而在网页显示位乱码的处理办法(非原创)
- 如何在SpringBoot下读取自定义properties配置文件
- springBoot之配置文件的读取以及过滤器和拦截器的使用
- SpringBoot----读取配置文件中的属性
- 如何在SpringBoot下读取自定义properties配置文件
- springBoot之配置文件的读取以及过滤器和拦截器的使用
- 如何在SpringBoot下读取自定义properties配置文件
- SpringBoot学习——如何设置和读取配置文件中属性
- 如何在SpringBoot下读取自定义properties配置文件
- springboot在jar包外部读取配置文件
- SpringBoot配置文件读取错误异常
- 如何在SpringBoot下读取自定义properties配置文件
- Springboot读取配置文件控制台中文乱码解决
- springBoot在工具类里面读取配置文件里面的信息