zoukankan      html  css  js  c++  java
  • SpingBoot 属性加载

    属性加载顺序

    配置属性加载的顺序

    1. 开发者工具 `Devtools` 全局配置参数;
    2. 单元测试上的 `@TestPropertySource` 注解指定的参数;
    3. 单元测试上的 `@SpringBootTest` 注解指定的参数;
    4. 命令行指定的参数,如 `java -jar springboot.jar --name="demo"`;
    5. 命令行中的 `SPRING_APPLICATION_JSONJSON` 指定参数, 如 `java -Dspring.application.json='{"name":"demo"}' -jar springboot.jar`
    6. `ServletConfig` 初始化参数;
    7. `ServletContext` 初始化参数;
    8. JNDI参数(如 `java:comp/env/spring.application.json`);
    9. Java系统参数(`System.getProperties()`);
    10. 操作系统环境变量参数;
    11. `RandomValuePropertySource` 随机数,仅匹配:`ramdom.*`;
    12. JAR包外面的配置文件参数(`application-{profile}.properties(YAML)`)
    13. JAR包里面的配置文件参数(`application-{profile}.properties(YAML)`)
    14. JAR包外面的配置文件参数(`application.properties(YAML)`)
    15. JAR包里面的配置文件参数(`application.properties(YAML)`)
    16. `@Configuration`配置文件上 `@PropertySource` 注解加载的参数
    17. 默认参数(通过 `SpringApplication.setDefaultProperties` 指定)

    数字小的优先级越高,即数字小的会覆盖数字大的参数值。

    属性配置方式

     

    1. PropertyPlaceholderConfigurer:
      • <context:property-placeholder location="classpath:sys.properties" />
      • @Bean的方式
        @Bean
        public PropertyPlaceholderConfigurer propertiess() {
            PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
            Resource[] resources = new ClassPathResource[]{new ClassPathResource("sys.properties")};
            ppc.setLocations(resources);
            ppc.setIgnoreUnresolvablePlaceholders(true);
            return ppc;
        }
      • xml 的方式
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:sys.properties</value>
                </list>
            </property>
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>  
    1. 通过 springboot 扩展方式:
      @Bean
      public CommandLineRunner commandLineRunner() {
          return (args) -> {
              System.setProperty("name", "demo");
          };
      }
    2. 通过 @PropertySource 配置

      @PropertySource("classpath:sys.properties")
      @Configuration
      public class DemoConfig {
      }
    3. @SpringBootTest(value = { "name=javastack-test", "sex=1" })

    属性获取方式

    1. 占位符:${PlaceHolder}
    2. SpEL 表达式 #{}
    3. 通过 Environment 获取
      // 只有使用注解 @PropertySource 的时候可以用,否则会得到 null。
      @Autowired
      private Environment env;
       
      public String getUrl() {
          return env.getProperty("demo.jdbc.url");
      }  
    4. 通过 @Value 注入
      @Value("${demo.jdbc.url}")
      private String url;
    5. @ConfigurationProperties
      @Configuration
      @ConfigurationProperties(prefix = "demo.db")
      @Data
      public class DataBase {
          String url;
          String username;
          String password;
      }
  • 相关阅读:
    学习dubbo
    【Spring】SpringMVC配置文件
    Mac下git配置
    【Spring】入门HelloWorld
    【MySql】启动/停止
    Javaweb 编解码流程
    TensorFlow学习笔记1
    Nginx 代理配置
    【转】RPC介绍
    【dubbo】dubbo控制台搭建
  • 原文地址:https://www.cnblogs.com/wade-luffy/p/9860981.html
Copyright © 2011-2022 走看看