写在前面:在我们的spring-boot的项目中有一个默认的配置文件:application.properties.程序能够自动读取这里面的配置信息,就比如需要指定服务启动的端口为8801,直接在配置文件中写:server.port=8801,然后启动就会发现确实是占用的8801端口。但有时我们需要自定义文件,不写在默认的配置文件application.properties中,所以需要了解自定义文件的读取。下面主要介绍对自定义的properties文件的读取,通过java bean 的方式绑定读取自定义文件。PS:下一篇介绍yml文件的读取
读取自定义properties文件
测试读取自定义主要涉及三个文件:define.properties(自定义文件)、DefineEntity.java(自定义文件绑定的java bean)、DefineController.java(测试效果)。
文件目录如下:
1:define.properties文件
definetest.pname=test
definetest.password=test123
definetest.TestUpcase = up
definetest.manyTo = many
define.one =one
2:DefineEntity.java文件
package com.wch.SpringBootTestDemo.defineTest;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author WanChengHe
* 对于下面三个注解:
* 第一个注解是为了让这个java bean可以在springboot项目启动时被扫描并加载导spring容器中
* 第二个注解:一般不单独使用,与第三个注解一起用,作用就是将指定的前缀的配置项的值与java bean 的字段绑定
* 为了绑定的成功,一般将字段的名称与配置项键的最后一个键名相同,这样整个键在去掉前缀的情况下就和字段名称一致,以此来进行绑定
* 第三个注解:指定绑定的自定义配置文件
*
*/
@Component
@ConfigurationProperties(prefix="definetest")
@PropertySource("classpath:define.properties")
public class DefineEntity {
private String pname;
private String password;
private String TestUpcase;
private String testOnly;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTestUpcase() {
return TestUpcase;
}
public void setTestUpcase(String testUpcase) {
TestUpcase = testUpcase;
}
public String getTestOnly() {
return testOnly;
}
public void setTestOnly(String testOnly) {
this.testOnly = testOnly;
}
}
3:DefineController.java文件
apackage com.wch.SpringBootTestDemo.defineTest;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/define")
public class DefineController {
@Autowired
private DefineEntity defineEntity;
@Autowired
private DefineYml defineYml;
/**
* 这里测试自定义文件
* 测试读取自定义properties形式文件内容
* @return
*/
@RequestMapping("/getDefine")
@ResponseBody
public String getDefineInfo(){
String str = "test.name="+defineEntity.getPname()
+"----test.password="+defineEntity.getPassword()
+"---test.testUp="+defineEntity.getTestUpcase()
+"testOnly="+defineEntity.getTestOnly();
return str;
}
}
测试效果:
通过java bean 的方式绑定读取自定义properties文件
* 在使用的过程中发现对配置文件中的命名前缀规范性特别强,
* 当前缀中有大写字母时系统跑不起来,前缀要全部使用小写,后面的大小写都可以
* 配置文件中可以有多个不同前缀的信息,我们只根据我们绑定的java bean选的前缀读取,其他的不影响(例:define.one = one)
* 或者我们在读取的时候只读取指定前缀下的某些属性,相同前缀下其他字段也不会有影响的(例:definetest.manyTo = many)
* 在java bean中也可以定义配置文件以外的字段,也是可以的,只是就没有值,需要单独为他赋值,不然打印出来就是null(例:本页中testOnly)
*
* 注意:像我们默认的配置文件 application.properties即使是我们不做任何处理,
* 系统也能够直接读取到,会自动加载,但是我们自定义的配置文件就需要我们手动加载。
PS:觉得还可以的话可以评论加关注噢,一起学习,一起成长!!!