Spring获取application.properties
方法一:@Value获取属性值
首先在application.properties中添加属性值
app.name=MyApp
app.description=${app.name} is a Spring Boot application
编写工具类,通过注解@Value获取属性值(类名这里就不讲究命名规范了)
package com.example.spdemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Mytest {
@Value("${app.name}")
private String appName;
@Value("${app.description}")
private String appDescription;
public String getAppName() {
return appName;
}
public String getAppDescription() {
return appDescription;
}
}
调用工具类,输出属性值
package com.example.spdemo.controller;
import com.example.spdemo.Application;
import com.example.spdemo.Mytest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Index {
@Autowired
private Mytest mytest;
@RequestMapping("/")
public String HelloWorld(){
System.out.println();
System.out.println(mytest.getAppName());
System.out.println(mytest.getAppDescription());
return "Hello World !";
}
}
方法二:@ConfigurationProperties
package com.example.spdemo.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@ConfigurationProperties(prefix = "app")
public class Index {
private String name;
private String description;
@RequestMapping("/")
public String getProps() {
return name + "|" + description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
如果配置没有app的前缀的话,去掉注解中的(prefix = "app"),保持属性名与类成员属性名一致即可。
Spring获取application.properties的更多相关文章
- 【转】spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in
Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...
- Spring boot application.properties 配置
原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...
- spring boot application.properties 属性详解
2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...
- Spring 的application.properties项目配置与注解
一.项目结构介绍 如上图所示,Spring Boot的基础结构共三个文件: src/main/java 程序开发以及主程序入口 src/main/resources 配置文件 src/test/ja ...
- spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- Spring boot application.properties和 application.yml 初学者的学习
来自于java尚硅谷教程 简单的说这两个配置文件更改配置都可以更改默认设置的值比如服务器端口号之类的,只需再文件中设置即可, properties可能是出现的比较早了,如果你不调你的默认编码,中文可能 ...
- spring boot application.properties详解
附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...
- spring boot application.properties乱码问题
1. 在application.properties 中增加 spring.http.encoding.force=true spring.http.encoding.charset=UTF- spr ...
随机推荐
- 装饰模式Decorator Pattern
1.主要优点 装饰模式的主要优点如下: (1) 对于扩展一个对象的功能,装饰模式比继承更加灵活性,不会导致类的个数急剧增加. (3) 可以对一个对象进行多次装饰,通过使用不同的具体装饰类以及这些装饰类 ...
- Host does not support domain type kvm for virtualization type 'hvm' arch 'x86_64'
kvm创建虚拟机报错: qemu-img create -f qcow2 /tmp/centos.qcow2 10G virt-install --virt-type kvm --name cento ...
- linux应用之gcc环境的安装
使用如下两个命令就可把gcc,gcc-c++统统安装 yum -y install gcc yum -y install gcc-c++ 但是用yum install gcc安装gcc编译环境,关于 ...
- php 内网/外网ip判断
工作需要判断ip是否是内网ip,本来想着使用正则自己写一个呢,后来发现php自带的有现成的函数[filter_var()](http://php.net/manual/zh/function.filt ...
- JMeter ----与WebDriver安装与测试
JMeter ----与WebDriver安装与测试 主要内容 JMeter安装 WebDriver安装 一个简单的JMeter+WebDriver示例 环境与参考 jvm版本: 1.8.0_65 j ...
- ASP.NET AJAX入门系列(9):在母版页中使用UpdatePanel
本文简单介绍一下在母版页中使用UpdatePanel控件,翻译自官方文档. 主要内容 1.添加UpdatePanel控件到Content Page 2.通过Master Page刷新UpdatePan ...
- 让shell脚本中的echo输出带颜色
转载自:http://www.linuxidc.com/Linux/2014-12/110463.htm 让echo输出带颜色的方法有两种 1.在shell脚本中定义颜色变量,使用echo -e调用变 ...
- Linux下批处理文件编写
linux下的批处理文件,基本就是shell脚本文件. 一.最简单的脚本书写方法为: 1.新建一个文件,名字为test(自己定义的名字) touch test.sh 2.在里面编写脚本 程序必须以下面 ...
- InfluxDB 1.6文档
警告!此页面记录了不再积极开发的InfluxDB的早期版本.InfluxDB v1.7是InfluxDB的最新稳定版本. InfluxDB是一个时间序列数据库,旨在处理高写入和查询负载.它是TICK堆 ...
- HTML5 Canvas水波纹动画特效
HTML5的Canvas特性非常实用,我们不仅可以在Canvas画布上绘制各种图形,也可以制作绚丽的动画,比如这次介绍的水波纹动画特效.以前我们也分享过一款基于HTML5 WebGL的水波荡漾动画,让 ...