zoukankan      html  css  js  c++  java
  • Spring-Boot导入配置文件与取值

    前言:

    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注解取配置文件中的值

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

     

    尾声:

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

  • 相关阅读:
    操作正则表达式的公共类
    验证码类
    强制转化常用函数
    实现Base64编码与其它编码转换的类
    关于任务管理器踩过的坑,程序员必看!
    每次找Internet选项感到抓狂?一键打开!
    教你避过安装TensorFlow的两个坑
    HTML中id与name的通俗区别
    VS code代码对齐快捷键
    form和table的区别
  • 原文地址:https://www.cnblogs.com/hellxz/p/8763596.html
Copyright © 2011-2022 走看看