本文摘自CSDN-曲健磊,原文地址:https://blog.csdn.net/a909301740/article/details/81448783
Spring如何获取配置在application.properties文件中属性的值?
通过 @Value("${com.springboot.name}")注解的方式:
application.properties 文件的内容如下:
com.springboot.name=qujianlei
com.springboot.title=studyhadoop
com.springboot.desc=${com.springboot.name} work hard ${com.springboot.title}
com.springboot.random.string=${random.value}
com.springboot.random.int=${random.int}
com.springboot.random.long=${random.long}
比方说我们现在要在 Controller 中获取 com.springboot.name 的值,只需像下面这样既可:
package com.qjl.bootstart.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qjl.bootstart.yml.User;
import com.qjl.bootstart.yml.YmlUtil;
/**
* 类名称:SpringMVC的控制器
* 全限定性类名: com.qjl.bootstart.controller.BootController
* @author 曲健磊
* @date 2018年8月4日上午10:38:23
* @version V1.0
*/
@RestController
@RequestMapping("/boot")
public class BootController {
@Value("${com.springboot.name}")
private String name;
@Value("${com.springboot.title}")
private String title;
@Value("${com.springboot.desc}")
private String desc;
@Value("${com.springboot.random.string}")
private String str;
@Value("${com.springboot.random.int}")
private Integer integer;
@Value("${com.springboot.random.long}")
private Long _long;
@RequestMapping("/hello")
public String boot() {
StringBuffer buffer = new StringBuffer();
buffer.append("hello SrpingBoot properties<br/>");
buffer.append("[").append(name).append("]<br/>");
buffer.append("[").append(title).append("]<br/>");
buffer.append("[").append(desc).append("]<br/>");
return buffer.toString();
}
@RequestMapping("/random")
public String testRandom() {
StringBuffer buffer = new StringBuffer();
buffer.append("hello SrpingBoot random<br/>");
buffer.append("[").append(str).append("]<br/>");
buffer.append("[").append(integer).append("]<br/>");
buffer.append("[").append(_long).append("]<br/>");
return buffer.toString();
}
}
相关阅读 >>
spring如何获取配置在application.properties文件中属性的值?
spring、springmvc、springboot和springcloud的区别
更多相关阅读请进入《spring》频道 >>

深入理解Java虚拟机 JVM高级特性与实践 周志明 第3版
这是一部从工作原理和工程实践两个维度深入剖析JVM的著作,是计算机领域公认的经典。