[Spring boot] Read values from external properties file
Let's say we have a extral app.proporites file which contains some extra configuration:
// resources/app.properties
external.url="http://somedomain.com"
We can read the extra propoties by using @Value("${xxx}")
package com.example.in28minutes.properties; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class SomeExternalService { @Value("${external.url}")
private String url; public String returnServiceURL () {
return url;
}
}
As you can see, we didn't define where should we looking for "app.proporties" file, this is what we should do in main file by @PropertySource("")
package com.example.in28minutes; import com.example.in28minutes.properties.SomeExternalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource; @SpringBootApplication
@PropertySource("app.properties")
public class In28minutesPropotiesApplication { private static Logger LOGGER = LoggerFactory.getLogger(In28minutesPropotiesApplication.class); public static void main(String[] args) {
// Application Context
ApplicationContext applicationContext =
SpringApplication.run(In28minutesPropotiesApplication.class, args);
SomeExternalService someService = applicationContext.getBean(SomeExternalService.class); LOGGER.info("{}", someService.returnServiceURL());
}
}
[Spring boot] Read values from external properties file的更多相关文章
- Spring Boot + Jersey发生FileNotFoundException (No such file or directory)
我在使用Spring Boot + Jersey 项目,解决了上一篇随笔中的FileNotFoundException,然后又报了一个FileNotFoundException,不过报错信息不一样了 ...
- 【spring boot】使用@Value映射properties文件属性
描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...
- 在spring boot中使用自定义的properties
1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...
- spring boot 在框架中注入properties文件里的值(Spring三)
前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...
- Spring Boot属性配置文件:application.properties 详解
学习资料 网址 官方说明文档 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-pro ...
- spring boot 1.4.1 with jsp file sample
<!--pom.xml--> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- Spring Boot中注入配置文件application.properties中的list 对象参数
例如要注入下列参数: dyn.spring.datasources[0].name=branchtadyn.spring.datasources[0].driverClassName=oracle.j ...
- Spring Boot 配置文件详解:Properties和YAML
一.配置文件的生效顺序,会对值进行覆盖: 1. @TestPropertySource 注解 2. 命令行参数 3. Java系统属性(System.getProperties()) 4. 操作系统环 ...
- spring boot mybatis XML文件读取properties配置信息
配置文件application.properties中相关配置信息可以在部署以后修改,引用配置信息可以在代码和mybatis的映射文件中 1.JAVA代码 可以通过变量去读取 application. ...
随机推荐
- delphi 主线程向子线程发送消息
while True do begin if not PeekMessage(msg,0,0,0,PM_REMOVE) then begin case MsgWaitForMultipleObject ...
- Delphi下让窗口不显示在任务栏的另类方法
刚才看到了这篇东西<使窗口不在任务栏上显示(利用ITaskbarList接口)>,作者用ITaskList接口实现了隐藏窗口在任务栏按钮的功能,想起我好多年以前做的程序也有这样的功能,但是 ...
- ntp测试
cmd下 w32tm /stripchart /computer:time1.aliyun.com linux ntpdate ntp1.aliyun.com
- EditText中文文档
感谢农民伯伯的翻译文:http://www.cnblogs.com/over140/archive/2010/09/02/1815439.html 属性名称 描述 android:autoLink 设 ...
- HTML5 Geolocation API地理定位整理(一)
HTML5 Geolocation API 用于获得用户的地理位置. 鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的. 浏览器支持 Internet Explorer 9+, ...
- Java(C#)基础差异-数组
1.填充数组 Java 数组填充替换方法Arrays.fill() 举例如下: import java.util.Arrays; public class FillDemo { public stat ...
- 图像质量评估(IQA)
图像质量评估函数的分类曾是一个比较有争议的话题,在2l世纪以前曾经有过 比较多的讨论.但是随着研究的深入和技术的广泛应用,研究人员对于图像质量 评估函数的分类有了统一的认识,即从实际应用中参考信息供给 ...
- Statistical Artifact (error)
In natural science and signal processing, an artifact is any error in the perception or representati ...
- [转]MySQL导入.sql文件及常用命令
From : http://blog.csdn.net/muziduoxi/article/details/6091202 在MySQL Qurey Brower中直接导入*.sql脚本,是不能一 ...
- TFS中查看我的所有签入迁出记录 TFS 怎么查看所有的修改
[源代码资源管理器]=>左边窗口目录树选中一行项目=>右键 查看历史记录=>在历史记录中,双击变更集即可以看到某一次变更的所有记录.