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 ...
随机推荐
- SPA项目中,404页面 和 登陆页面 对应的路由,应该怎样控制?
SPA项目中,404页面 和 登陆页面 对应的路由,应该怎样控制? 可以这样做: 登陆之前,所有页面跳到 登陆页面:包括随便输入的路由地址. 登陆后,跳到相应页面:随便输入的.不存在的路由地址,才跳到 ...
- vue-router 与 react-router 设计理念上的区别
vue-router 与 react-router 设计理念上的区别: 区别 vue-router react-router 改成history mode: 'history' 直接使用 react- ...
- golang 查看代码调用关系图
go-callvis 是github上一个开源项目,可以用来查看golang代码调用关系. 安装 安装graphviz $ brew install graphviz 安装go-callvis go ...
- Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计
Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计 一提到hadoop相信熟悉IT领域或者经常关注互联网新闻的朋友都应该很熟悉了,当然,这种熟悉可能也只是听着名字耳熟,但并不知道它具体是什 ...
- 关于nginx大流量负载调优
优化nginx包括两方面: 1.是自己重写nginx代码(比如tengine).本身nginx的代码已经足够优秀,如果不是每秒几千的请求,就忽略这个部分吧. 2.另一个就是和优化nginx的配置,这是 ...
- ALGO-141_蓝桥杯_算法训练_P1102
定义一个学生结构体类型student,包括4个字段,姓名.性别.年龄和成绩.然后在主函数中定义一个结构体数组(长度不超过1000),并输入每个元素的值, 程序使用冒泡排序法将学生按照成绩从小到大的顺序 ...
- STL基础--迭代器和算法
1 迭代器 Iterators 5种迭代器类型 随机访问迭代器: vector, deque, array // 允许的操作 vector<int> itr; itr = itr + 5; ...
- Jenkins小试
之前有提到和同事搭建了个Git+Gerrit+Jenkins环境,可惜都在一台机器上,中间IT重装系统后就杯具了,没有备份,只好重来. 6月份项目发布了首个Open API,那时候建了个api uni ...
- java高并发编程(四)高并发的一些容器
摘抄自马士兵java并发视频课程: 一.需求背景: 有N张火车票,每张票都有一个编号,同时有10个窗口对外售票, 请写一个模拟程序. 分析下面的程序可能会产生哪些问题?重复销售?超量销售? /** * ...
- Hive 的基本概念
Hadoop开发存在的问题 只能用java语言开发,如果是c语言或其他语言的程序员用Hadoop,存在语言门槛. 需要对Hadoop底层原理,api比较了解才能做开发. Hive概述 Hive是基于H ...