您的位置:首页 > 编程语言 > Java开发

【系统学习SpringBoot】SpringBoot读取Property配置

2017-09-04 18:03 627 查看
SpringBoot读取Property

在web开发的过程中,经常需要自定义一些配置文件作为全局配置(数据库连接之类的)。

在SpringBoot中,读取property文件会很简单。

真的很简单,,不信看下面 ▼.▼

【1】在application.yml中添加追加如下配置(yml格式)

#yml语法比起 properties更加方便,,,
xatu:
zsl:
name: 小鼠标
age: 21
id: 10086


【2】编写获取配置信息的类

package xatu.zsl.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* Created by zsl on 2017/9/4.
*/
@Component
public class XatuZSLinfo {
@Value("${xatu.zsl.name}")
private String name;
@Value("${xatu.zsl.id}")
private String id;
@Value("${xatu.zsl.age}")
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}


【3】搞个测试方法,试试

@Autowired
private XatuZSLinfo properties;

@GetMapping("/getzsl")
public XatuZSLinfo getXatuZSLinfo() {
return properties;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息