zoukankan      html  css  js  c++  java
  • 属性配置

    SpringApplication设置properties

    @SpringBootApplication
    public class SpringBootDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication springApplication = new SpringApplication(SpringBootDemoApplication.class);
            Properties properties = new Properties();
            properties.setProperty("fly.url","fly_url1");
            springApplication.setDefaultProperties(properties);
            springApplication.run(args);
        }
    }
    

    使用@PropertySource

    
    @SpringBootApplication
    @PropertySource({"demo.properties"})//低于application
    public class SpringBootDemoApplication {
        public static void main(String[] args) {
            SpringApplication springApplication = new SpringApplication(SpringBootDemoApplication.class);
            springApplication.run(args);
        }
    }
    

    application.properties

    # 自定义属性  优先于 application.yml
    #fly.url=fly_url2
    

    application.yml

    # 自定义属性
    fly:
      url: fly_url4
    

    application-default.yml

    application-default.properties

    测试

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.context.EnvironmentAware;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Component;
    
    /**
     * 启动器加载属性配置
     */
    @Component
    public class ResultCommandLineRunner implements
            CommandLineRunner, EnvironmentAware {
        private Environment environment;
        @Override
        public void run(String... args) throws Exception {
            System.out.println(environment.getProperty("fly.url"));
            System.out.println(environment.getProperty("fly.avg.age"));
        }
    
        @Override
        public void setEnvironment(Environment environment) {
            this.environment = environment;
        }
    }
    

    springboot测试中定义

    //@SpringBootTest(properties = {"fly.url=fly_url9"})
    @SpringBootTest
    @TestPropertySource({"classpath:demo.properties"})
    public class Pro {
    
        @Test
        public void test(){
        }
    }
    

    可使用动态变量

    fly.avg.age=${random.int[20,30]}
    
  • 相关阅读:
    求正整数N(N>1)的质因数的个数。
    手机键盘输入字母
    第二部分进度
    第一部分:地域维度标准化
    利用python解析地址经纬度
    输入任意4个字符(如:abcd), 并按反序输出(如:dcba)
    python-->微信支付
    python-图片流传输(url转换二维码)
    python-qrcode-二维码
    ajax和axios、fetch的区别
  • 原文地址:https://www.cnblogs.com/fly-book/p/12704917.html
Copyright © 2011-2022 走看看