详解Spring Boot加载properties和yml配置文件
作者:赛亚人之神 发布时间:2023-11-24 07:14:09
标签:spring,boot,yml
一、系统启动后注入配置
package com.example.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
/**
* @author: GrandKai
* @create: 2016-09-01 11:24
*/
@Configuration
@PropertySource(ignoreResourceNotFound = true, value = {"classpath:/config/email.properties","classpath:/config/email.yml"}, name = "email")
public class Config {}
需要在ApplicationContext中注册配置
AnnotationConfigEmbeddedWebApplicationContext context = (AnnotationConfigEmbeddedWebApplicationContext) app.run("参数1");
context.register(Config.class);
用以下方式取值
Environment env = context.getEnvironment();
System.out.println(env.getProperty("address"));
email.yml文件配置如下:
server:
address: 127.0.0.1
二、在命令行传入注入到程序中
public class Main {
public static void main(String... args) {
//initialize the command line parsing stuff
OptionParser parser = new OptionParser();
parser.accepts("greeting").withRequiredArg();
OptionSet options = parser.parse(args);
//create the actual Spring PropertySource
PropertySource<?> ps = new JOptCommandLinePropertySource(options);
//setup the Spring context
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().getPropertySources().addLast(ps);
//register the property source with the environment
ctx.register(Greeter.class);
ctx.refresh();
Greeter greeter = ctx.getBean(Greeter.class);
greeter.sayGreeting();
}
}
@Component
class Greeter {
@Inject private Environment env;
//the following would also work
//@Value("${greeting}")
//private String greeting;
/**
* Print out the 'greeting' property if it exists, and otherwise, "Welcome!".
*/
public void sayGreeting() {
System.out.println(env.getProperty("greeting", "Welcome!"));
}
}
public static void main(String [] args) {
SimpleCommandLinePropertySource ps = new SimpleCommandLinePropertySource(args);
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().getPropertySources().addFirst(ps);
ctx.register(ApplicationConfig.class);
ctx.refresh();
}
@Configuration
@EnableScheduling
@ComponentScan("com.mycompany.package")
@PropertySource(
value = {"classpath:/application.properties", "file:${config.location}"},
ignoreResourceNotFound = true
)
class ApplicationConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@Component
class MyComponent {
@Value("${my.property.data}")
private String myPropertyData;
@Scheduled(fixedDelayString = "${schedule.delay.period}")
public void run() {
:
}
}
来源:http://www.jianshu.com/p/eadfecea1f2d


猜你喜欢
- 今天在项目中用到了用到了一种特殊的EditText,当用户在EditText中输入内容,点击搜索按钮的时候,输入的内容能够高亮,然后添加到输
- 1. 概述官方JavaDocsApi: javax.swing.JFrameJFrame,窗口。JFrame 是一个可以独立显示的组件,一个
- @Autowired注解和静态方法及new关系一、@autowired 与new new出来的对象无法调用@Autowired注入
- 一、通过程序看现象在开始为大家讲解Java 多线程缓存模型之前,我们先看下面的这一段代码。这段代码的逻辑很简单:主线程启动了两个子线程,一个
- 目录MotionEventViewViewGroup事件拦截寻找目标视图,分发ACTION_DOWN分发除ACTION_DOWN外的其他事件
- Swagger是一款遵循 Restful 风格的接口文档开发神器,支持基于 API 自动生成接口文档,接口文档始终与 API 保持同步,不再
- 本文实例为大家分享了C#字数统计(字母、数字、汉字、符号)的具体代码,供大家参考,具体内容如下namespace 测试1{ public p
- 之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘;Java实现两人五子棋游戏(三) 画出棋子;Java实现两人五子棋游戏(四) 落
- 一、了解面向对象1、概念基本理解:1)、一个个体可以看做是一个对象,例如:人这个个体;2)、有共同属性的一类作为一个个体,例如:学生、白领、
- 目录知识点介绍正文1、质量压缩2、采样率压缩3、缩放法压缩4、RGB_565 通过改变图片格式来实现压缩总结知识点介绍Android 中图片
- 昨天晚上写代码的时候偶然发现 DateTime 里出现了星期几,当时一阵凌乱,去网上百度没有详细解决办法,很多人说可以用用 ToString
- 为什么是MVI而不是MVVMMVVM作为流行的架构模式,应用在 Compose上,并没有大的问题或者设计缺陷。但是在使用期间,发现了并不适合
- 在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的File
- 背景公司最近要求给我负责的APP加上视频录制和发布的功能,我简单的完成了基本的录制和视频压缩功能,后来发现发布接口需要上传视频的截图,网上搜
- 状态分类在Hibernate框架中,为了管理持久化类,Hibernate将其分为了三个状态:瞬时态(Transient Object)持久态
- 命令行编译java文件import java.util.*;public class shuchu{ public
- 导入Jstl标签库<%@ taglib uri="http://java.sun.com/jsp/jstl/core&quo
- package com.cooly;import java.util.LinkedList;/*** @author coolyqq*模拟打
- 之前花了几天去研究怎么使用netty做一个网关服务器,虽然最后还是没能用上我做的网关,但是呢netty是会用了,总结一下netty和spri
- 问题onClick事件是Android开发中最常见的事件。比如,一个submitButton,功能是点击之后会提交一个订单,则一般代码如下,