一、系统启动后注入配置
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() {
:
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring
# boot
# 加载yml
# yml配置
# springboot
# yml
# SpringBoot配置文件properties和yml的实现
# SpringBoot获取yml和properties配置文件的内容
# Spring Boot 配置文件类型properties 格式与yml 格式
# SpringBoot配置文件方式
# 在线yml文件转properties
# 命令行
# 大家多多
# 系统启动
# ApplicationContext
# Config
# public
# register
# run
# app
# AnnotationConfigEmbeddedWebApplicationContext
# classpath
# true
# ignoreResourceNotFound
# email
# properties
# getEnvironment
# static
# Main
# void
相关文章:
中山网站推广排名,中山信息港登录入口?
建站VPS选购需注意哪些关键参数?
如何快速搭建高效WAP手机网站?
如何做网站制作流程,*游戏网站怎么搭建?
创业网站制作流程,创业网站可靠吗?
网站好制作吗知乎,网站开发好学吗?有什么技巧?
建站主机是否等同于虚拟主机?
官网自助建站系统:SEO优化+多语言支持,快速搭建专业网站
如何在Ubuntu系统下快速搭建WordPress个人网站?
制作网站怎么制作,*游戏网站怎么搭建?
在线教育网站制作平台,山西立德教育官网?
建站主机服务器选型指南与性能优化方案解析
制作销售网站教学视频,销售网站有哪些?
如何在Windows服务器上快速搭建网站?
建站之星伪静态规则如何正确配置?
建站主机CVM配置优化、SEO策略与性能提升指南
C#怎么使用委托和事件 C# delegate与event编程方法
如何在橙子建站上传落地页?操作指南详解
如何在Windows环境下新建FTP站点并设置权限?
javascript中的try catch异常捕获机制用法分析
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
c# await 一个已经完成的Task会发生什么
电商平台网站制作流程,电商网站如何制作?
专业网站制作服务公司,有哪些网站可以免费发布招聘信息?
一键制作网站软件下载安装,一键自动采集网页文档制作步骤?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
Python路径拼接规范_跨平台处理说明【指导】
制作假网页,招聘网的薪资待遇,会有靠谱的吗?一面试又各种折扣?
建站之星上传入口如何快速找到?
PHP 500报错的快速解决方法
浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?
厦门模型网站设计制作公司,厦门航空飞机模型掉色怎么办?
专业网站建设制作报价,网页设计制作要考什么证?
网页制作模板网站推荐,网页设计海报之类的素材哪里好?
建站主机选购指南与交易推荐:核心配置解析
建站三合一如何选?哪家性价比更高?
C++中引用和指针有什么区别?(代码说明)
西安大型网站制作公司,西安招聘网站最好的是哪个?
如何撰写建站申请书?关键要点有哪些?
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
如何安全更换建站之星模板并保留数据?
宁波免费建站如何选择可靠模板与平台?
*服务器网站为何频现安全漏洞?
黑客如何利用漏洞与弱口令入侵网站服务器?
贸易公司网站制作流程,出口贸易网站设计怎么做?
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?
如何在IIS中新建站点并解决端口绑定冲突?
购物网站制作公司有哪些,哪个购物网站比较好?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
*请认真填写需求信息,我们会在24小时内与您取得联系。