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 ...
随机推荐
- SQL数据库简单操作
sql语言简介 (1)数据库是文件系统,使用标准sql对数据库进行操作 * 标准sql,在mysql里面使用语句,在oracle.db2都可以使用这个语句 (2)什么是sql * Structured ...
- Spring Cloud(Dalston.SR5)--Hystrix 断路器-合并请求
在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的合并请求,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创 ...
- mac下安装、配置redies
https://blog.csdn.net/qq_21383435/article/details/80676497 可视化客户端安装(Mac): ruby -e "$(curl -fsSL ...
- Linux命令之sed
sed命令格式 sed [options] 'command' file(s) 选项 -e :直接在命令行模式上进行sed动作编辑,此为默认选项; -f :将sed的动作写在一个文件内,用–f fil ...
- Delphi实现树型结构
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- oracle rename数据文件的两种方法
oracle rename数据文件的两种方法 2012-12-11 20:44 10925人阅读 评论(0) 收藏 举报 分类: oracle(98) 版权声明:本文为博主原创文章,未经博主允许不 ...
- PHP $a='abcdef';请取出$a的值并打印第一个字母
echo $a[0];echo $a{0};echo chr(ord($a));echo substr( $a, 0,1);
- 数据仓库专题18-数据建模语言IDEF(转载)
1引言 IDEF的含义是集成计算机辅助制造(Integrated Computer-AidedManufacturing,ICAM)DEFinition.最初的IDEF方法是在美国空军ICAM项目建立 ...
- react事件中的事件对象和常见事件
不管是在原生的js还是vue中,所有的事件都有其事件对象,该事件对象event中包含着所有与事件相关的信息,在react中,所有的事件也有其事件对象,在触发DOM上的某个事件时,就会产生一个事件对象. ...
- Python:删除、增加字典的元素
一)增加一个或多个元素 d = {'a': 1} d.update(b=2) #也可以 d.update({‘b’: 2}) print(d) -->{'a': 1, 'b': 2} ...