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 ...
随机推荐
- python引入自定义模块
Python的包搜索路径 Python会在以下路径中搜索它想要寻找的模块:1. 程序所在的文件夹2. 标准库的安装路径3. 操作系统环境变量PYTHONPATH所包含的路径 将自定义库的路径添加到Py ...
- Maven项目main和test文件夹说明
需要自己来手动调整项目目录, Maven项目通常划分为 main 和 test 两部分,main 中存放实际项目资源,test 存放测试项目资源,二者内部同时又划分为 source 和 resourc ...
- LOJ 2743(洛谷 4365) 「九省联考 2018」秘密袭击——整体DP+插值思想
题目:https://loj.ac/problem/2473 https://www.luogu.org/problemnew/show/P4365 参考:https://blog.csdn.net/ ...
- HanLP代码与词典分离方案与流程
之前在spark环境中一直用的是portable版本,词条数量不是很够,且有心想把jieba,swcs词典加进来, 其他像ik,ansi-seg等分词词典由于没有词性并没有加进来. 本次修改主要是采用 ...
- button高度改变
代码:<input type="button" name="submit" value="submit" /> 利用css改变b ...
- linux二进制安装MariaDB
第一步:准备账号 [root@centos7 ~]#getent passwd mysql //查看有没有mysql账号(没有的话需要创建) [root@centos7 ~]#useradd -r m ...
- line-height的理解
font-size:0清除display:inline-block元素换行符间隙,比如两个img标签之间有换行符 行内元素的高度是由其行高决定的. Div或者其他元素内的图片,底部会有间隙,原因是图片 ...
- 不可小视的String字符串
String印象 String是java中的无处不在的类,使用也很简单.初学java,就已经有字符串是不可变的盖棺定论,解释通常是:它是final的. 不过,String是有字面量这一说法的,这是其他 ...
- IDEA运行tomcat8.5.35源代码
前提环境,安装和配置好java1.8+环境,maven,IDEA 1.下载Tomcat源代码:https://tomcat.apache.org/download-80.cgi#8.5.35 2.创建 ...
- [蓝桥杯ALGO-53.算法训练_最小乘积(基本型)
问题描述 给两组数,各n个. 请调整每组数的排列顺序,使得两组数据相同下标元素对应相乘,然后相加的和最小.要求程序输出这个最小值. 例如两组数分别为: -5和- 那么对应乘积取和的最小值应为: (-) ...