[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. ...
随机推荐
- GO语言基础之method
方法 method 1. Go 中虽没有 class,但依旧有 method 2. 通过显示说明 receiver 来实现与某个类型的组合 3. 只能为同一个包中的类型定义方法 4. Receiver ...
- linux 内核升级 转
inux 内核升级 2011-03-25 23:13:28 分类: LINUX 因要测试一些软件,需要2.6.30以上的内核,安装好CentOS 5.5,内核是2.6.18-194.el5.这次的升级 ...
- 网速4M等于多少KB/S,等于多少kbps
4M=512KB/S=4096Kbps 1KB/S=8Kbps 8倍速 转:http://zhidao.baidu.com/link?url=8GAyhcY9BbVstQr8pE3I7QP_M53Km ...
- Revit API创建几何实体Solid并找到与之相交的元素
几何实体的创建方法之一:构成封闭底面,指定拉伸方向与拉伸高度.GeometryCreationUtilities ; , pt.Y - dBoxLength / , pt.Z); ...
- 使用JavaScript的数组实现数据结构中的队列与堆栈
今天在项目中要使用JavaScript实现数据结构中的队列和堆栈,这里做一下总结. 一.队列和堆栈的简单介绍 1.1.队列的基本概念 队列:是一种支持先进先出(FIFO)的集合,即先被插入的数据,先被 ...
- CAD扩展属性的提取--FME方式
一.CAD的扩展属性 了解一下CAD的扩展属性方式,CAD的扩展属性包括二类: 基于CAD二次开发的软件产品(例如南方cass),其扩展属性是附属在图形(点.多段线.注记.面)上面的,它是以XReco ...
- IOS文件系统及其相关操作(NSFileManager,NSFileHandle)
How do you get the paths to these special sandbox directories? NSArray *NSSearchPathForDirectoriesIn ...
- JMeter学习(二十三)关联
话说LoadRunner有的一些功能,比如:参数化.检查点.集合点.关联,Jmeter也都有这些功能,只是功能可能稍弱一些,今天就关联来讲解一下. JMeter的关联方法有两种:后置处理器-正则表达式 ...
- python测试开发django-20.添加创建时间DateTimeField
前言 我们在admin后台发布一篇文章的时候,一般会有创建时间和最后更新时间这2个字段,创建时间就是第一次编辑文章的时候自动添加的,最后更新时间就是每次修改文章的内容后自动更新 在models.py建 ...
- svn导出文件进行比较
之前有介绍svn log 的命令,即可导出版本A~B之间所有的修改动作,然后复制出相应的文件(中间有一个算法去处理每一个动作,然后得到最终需要导出的文件列表,svn常用动作有:Modified.Add ...