一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring Boot加載properties和yml配置文件

詳解Spring Boot加載properties和yml配置文件

2020-09-10 14:15賽亞人之神 Java教程

本篇文章主要介紹了詳解Spring Boot加載properties和yml配置文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、系統(tǒng)啟動后注入配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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中注冊配置

?
1
2
AnnotationConfigEmbeddedWebApplicationContext context = (AnnotationConfigEmbeddedWebApplicationContext) app.run("參數(shù)1");
context.register(Config.class);

用以下方式取值

?
1
2
Environment env = context.getEnvironment();
System.out.println(env.getProperty("address"));

email.yml文件配置如下:

?
1
2
server:
 address: 127.0.0.1

二、在命令行傳入注入到程序中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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() {
     :
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.jianshu.com/p/eadfecea1f2d

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: a毛片在线免费观看 | 手机在线观看精品国产片 | 免费人成网址在线观看国内 | 国产成人无精品久久久久国语 | 男人j放进女人的p视频免费 | 国产成人影院一区二区 | 免费高清www动漫视频播放器 | 亚洲一区二区三区在线播放 | 久久中文字幕免费高清 | 欧美一区二区不卡视频 | 好性20岁 | 亚洲免费视频一区 | 别停好爽好深好大好舒服视频 | 欧美一区二区三 | 日本不卡在线观看免费v | 日韩精选视频 | 亚洲日本视频在线观看 | 麻麻与子乱肉小说怀孕 | 精品丰满人妻无套内射 | 久久青草免费91线频观看站街 | 91久久国产露脸精品 | chinese帅男gayvideo| 韩国三级在线 | 欧美精品亚洲精品日韩1818 | 男人的天堂在线观看入口 | 日本成熟bbxxxxxxxx | 成年人在线观看视频 | 国产大胆歌舞团网站 | 四虎e234hcom| 日韩欧美不卡视频 | 国产欧美一区二区精品性色 | 帅老头恋帅老头同性tv | 娇小XXXXX第一次出血 | 亚洲黄视频在线观看 | 国产私人影院 | 小柔的性放荡羞辱日记动漫 | 国产九九视频在线观看 | 爽好舒服快想要免费看 | 国产精品久久久久久久牛牛 | 狠狠色综合久久久久尤物 | 我的奶头被客人吸的又肿又红 |