当前位置:   article > 正文

Spring-Boot导入配置文件与取值

springboot的import的value与location

前言:

springboot简化了大量配置文件,但是必要时还是需要导入配置文件的,比如dubbo,此处简记之。


正文:

所有的配置文件引入都是使用注解在类上进行引入的,常用的有两种注解@PropertySource和@ImportSource,分别导入properties文件和xml文件


@PropertySource注解

引入单个properties文件:

@PropertySource(value = {"classpath : xxxx/xxx.properties"})

引入多个properties文件:

@PropertySource(value = {"classpath : xxxx/xxx.properties","classpath : xxxx.properties"})


@ImportSource注解 :可以额外分为两种模式 相对路径classpath,绝对路径(真实路径)file

注意:单文件可以不写value或locations,value和locations都可用

相对路径(classpath):

引入单个xml配置文件:

@ImportSource("classpath : xxx/xxxx.xml")

引入多个xml配置文件:

@ImportSource(locations={"classpath : xxxx.xml" , "classpath : yyyy.xml"})

绝对路径(file):

引入单个xml配置文件:

@ImportSource(locations= {"file : d:/hellxz/dubbo.xml"})

引入多个xml配置文件:

@ImportSource(locations= {"file : d:/hellxz/application.xml" , "file : d:/hellxz/dubbo.xml"})


取值:

使用@Value注解取配置文件中的值

  1. @Value("${properties中的键}")
  2. private String xxx;

 

尾声:

如本文中有错误,还望评论告知一二,欢迎拍砖

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46819
推荐阅读
相关标签
  

闽ICP备14008679号