激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

服務器之家 - 編程語言 - Java教程 - spring boot 注入 property的三種方式(推薦)

spring boot 注入 property的三種方式(推薦)

2020-12-01 14:51778875 Java教程

這篇文章主要介紹了spring boot 注入 property的三種方式,需要的朋友可以參考下

以前使用spring的使用要注入property要配置PropertyPlaceholder的bean對象。在springboot除  了這種方式以外還可以通過制定 配置ConfigurationProperties直接把property文件的 屬性映射到 當前類里面。

?
1
@ConfigurationProperties(prefix = "mypro", merge = true, locations = { "classpath:my.properties" })

ConfigurationProperties prefix 屬性指示property文件中屬性的前綴是什么。我這里寫的是mypro。

因此property文件的屬性必須mypro.x.y=z的形式;

     配置好ConfigurationProperties 之后就可以把property文件的屬性映射到當前類了。

?
1
2
3
mypro.a:1
mypro.b:2
abc.d:123

property 文件里面mypro前綴的有a 和b兩個。因此我在當前類就可以新建這兩個屬性。

?
1
2
private int a;
private int b;

這些需要映射的屬性一定要加上getter 和setter。因為spring是通過反射調用方法來修改屬性值的

        以前使用spring注入property的方式也同樣適用。以前是xml配置PropertyPlaceholder。現在使用@bean 或者直接@Component配置這個類。只要把PropertyPlaceholderConfigurer添加到bean工廠,就可以使用@Value 取值了。

?
1
2
3
4
5
6
7
8
9
10
11
@Component
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
 public MyPropertyPlaceholderConfigurer(){
 this.setIgnoreResourceNotFound(true);
   final List<Resource> resourceLst = new ArrayList<Resource>();
   resourceLst.add(new ClassPathResource("my.properties"));
   this.setLocations(resourceLst.toArray(new Resource[]{}));
 }
}
@Value("abc.d")
 private String test;

        另外的一種方法跟第二種差不多的。更像以前的xml配置PropertyPlaceholder。只是現在的配置是用@Configuration標注的類,用@Bean標注要配置的bean對象;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class Testproperties {
 @Bean
 public PropertyPlaceholderConfigurer properties(){
 
 
 final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
   ppc.setIgnoreResourceNotFound(true);
   final List<Resource> resourceLst = new ArrayList<Resource>();
   resourceLst.add(new ClassPathResource("my.properties"));
   ppc.setLocations(resourceLst.toArray(new Resource[]{}));
   return ppc;
 }
}

以上所述是小編給大家介紹的spring boot 注入 property的三種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://my.oschina.net/u/778875/blog/1359216

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品国产精品亚洲 | 91av资源在线 | 欧美人xxx| 黄色片在线免费播放 | 日韩一级免费 | 欧产日产国产精品v | 99精品视频在线导航 | 99亚洲视频 | 久久久aa| 日韩av有码在线 | 在线 日本 制服 中文 欧美 | 国产成年人网站 | 国产亚洲精品久久久久婷婷瑜伽 | av电影观看 | 毛片免费试看 | 国产精品久久久久久久久久 | 亚洲成人黄色片 | 操碰网| 亚洲成a| 亚洲欧美在线视频免费 | 国产精品1区2区在线观看 | 成年免费观看视频 | 午夜一级| 免费国产 | 欧美性受xxxx人人本视频 | 国产一区二区在线免费播放 | 国产正在播放 | 中文字幕在线亚洲精品 | 精品一二三区视频 | h视频免费看| 亚洲精久久| 久久国产精品区 | 欧美不卡 | a级高清免费毛片av在线 | 免费黄色大片在线观看 | 99精品国产在热久久婷婷 | 成人免费福利 | 欧美在线观看视频一区二区 | 亚洲片在线观看 | 青草久久av | 国产精品aⅴ |