三种获取方法:
- 通过Environment对象获取
- 通过@ConfigurationProperties注解获取
- 使用@Value注解
通过Environment获取
在Controller 或者 Service 类中,使用@Autowired注解:
@Autowired
private Environment env;
使用getProperty(String key)获取具体的属性:
String keyValue = env.getProperty(key);
比如在application.properties文件中存储如下内容:
app.title=Learning Spring Boot
app.description=Working with properties file
在Controller中的使用示例如下:
package com.xarhsoft.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("app")
public class AppController {
@Autowired
private Environment env;
@GetMapping("/property")
public String getPropertyValue(@RequestParam("key") String key)
{
String returnValue = "No value";
//获取具体的属性
String keyValue = env.getProperty(key);
if( keyValue!= null && !keyValue.isEmpty())
{
returnValue = keyValue;
}
return returnValue;
}
}
使用@Value注解
使用@Value("${app.title}")直接获取:
如获取如下内容中的第一行属性值:
app.title=Learning Spring Boot
app.description=Working with properties file
获取方法如下:
@Value("${app.title}")
private String appTitle;
完整示例:
package com.xarhsoft.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("app")
//此处获取到值
@Value("${app.title}")
private String appTitle;
@GetMapping("/value")
public String getValue()
{
return appTitle;
}
}
使用@ConfigurationProperties注解
前例中定义的properties每个属性名称都以app前缀开头,所以我们需要用以下代码注解Java Bean:
@ConfigurationProperties("app")
完整示例:
package com.xarhsoft.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("app")
public class AppProperties {
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
一旦有了AppProperties类的实例,就可以使用getter获取存储在application.properties文件中的属性的值。
package com.xarhsoft.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("app")
public class AppController {
@Autowired
AppProperties myAppProperties;
@GetMapping("/title")
public String getAppTitle()
{
return myAppProperties.getTitle();
}
@GetMapping("/description")
public String getAppDescription()
{
return myAppProperties.getDescription();
}
}
本文介绍Spring Boot中三种常见的配置获取方式:通过Environment对象、@ConfigurationProperties注解及@Value注解。展示了如何从application.properties文件中读取配置并应用于Controller或Service类。
1159

被折叠的 条评论
为什么被折叠?



