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;
      }
  • 相关阅读:
    谈谈iOS开发如何写个人中心这类页面--静态tableView页面的编写
    H5活动产品设计指南基础版
    提高你的Java代码质量吧:使用valueof前必须进行校验
    最新VMware Workstation 10注册码,绝对可用!
    ORACLE 使用RMAN管理归档日志 archived log
    hdu 2072 单词数
    【早盘必读】9.13证券市场要闻(附股)
    Java程序性能优化
    CSS3媒体查询(Media Queries)
    [cocos2d-x]用CCSpriteBatchNode进行文理贴图的优化
  • 原文地址:https://www.cnblogs.com/wade-luffy/p/9860981.html
Copyright © 2011-2022 走看看