SpringBoot:4.在SpringBoot中获取application.properties内的值

本文介绍Spring Boot中三种常见的配置获取方式:通过Environment对象、@ConfigurationProperties注解及@Value注解。展示了如何从application.properties文件中读取配置并应用于Controller或Service类。

三种获取方法:

  1. 通过Environment对象获取
  2. 通过@ConfigurationProperties注解获取
  3. 使用@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();
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丷丩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值