java自定义配置文件xml-凯发k8官方网
环境依赖
sprint-boot version 2.1.7
java version 1.8
pom.xml 依赖jar包
org.springframework.boot
spring-boot-configuration-processor
true
test.properties
username=test
password=123456
info.age=26
看下目录布局
testconfig.java 加载配置的类文件
//prefix 为空则加载整个配置文件中的 参数
@configurationproperties(prefix = "")
@propertysource("classpath:test.properties")
//注意一定要注册
@component
public class testconfig {
private string username;
private string password;
private info info;
public void setinfo(info info) {
this.info = info;
}
public info getinfo() {
return info;
}
public string getusername() {
return username;
}
public string getpassword() {
return password;
}
public void setusername(string username) {
this.username = username;
}
public void setpassword(string password) {
this.password = password;
}
public static class info{
private integer age;
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
@override
public string tostring() {
return "info{"
"age=" age
'}';
}
}
@override
public string tostring() {
return "testconfig{"
"username='" username '\''
", password='" password '\''
", info=" info
'}';
}
}
hellocontroller.java 用来测试
@controller
public class hellocontroller {
@autowired
testconfig testconfig;
@responsebody
@getmapping("/hello")
public string sayhello()
{
//读取配置
return "age=" testconfig.getinfo().getage();
}
@responsebody
@getmapping("username")
public string getusername()
{
//读取配置测试
return testconfig.getusername();
}
}
总结
以上是凯发k8官方网为你收集整理的java自定义配置文件xml_spring-boot自定义配置文件的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java android统计图_andr
- 下一篇: java redis使用卡死_jedis