zoukankan      html  css  js  c++  java
  • spring-boot 属性定义和配置bean

    自定义bean属性

    1.定义bean属性

    // 通过@ConfigurationProperties加载properties文件内的配置,
    // 通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置
    @ConfigurationProperties(prefix = "com.dudu")
    public class ConfigBean {
        private String name;
        private String want;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getWant() {
            return want;
        }
    
        public void setWant(String want) {
            this.want = want;
        }
    }
    
    
    // 1.4版本的可以 通过@ConfigurationProperties加载properties文件内的配置,
    // 通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置
    
    //1.5版本后没有locations属性了,需要配合使用后@Configuration
    // 和@PropertySource("classpath:test.properties")来指定properties文件的位置
    @Configuration
    @ConfigurationProperties(prefix = "com.md")
    @PropertySource("classpath:test.properties")
    public class ConfigTestBean {
        private String name;
        private String want;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getWant() {
            return want;
        }
    
        public void setWant(String want) {
            this.want = want;
        }
    }
    

    2.在属性文件配置

    3.启用bean属性配置
    @EnableConfigurationProperties({ConfigBean.class, ConfigTestBean.class})

    在配置文件提示自定义属性

    1.加依赖

       <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    

    2.mvn compile

    指定配置文件位置

    1.可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置

    java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar 
    --spring.config.location=D:/application.properties
    

    2.外部配置加载顺序
    SpringBoot也可以从以下位置加载配置:优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置。

    1. 当前目录下的一个/config子目录
    2. 当前目录
    3. 一个classpath下的/config包
    4. classpath根路径(root)
      这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)

    1.命令行参数
    2.来自java:comp/env的JNDI属性
    3.Java系统属性(System.getProperties())
    4.操作系统环境变量
    5.RandomValuePropertySource配置的random.*属性值
    6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
    7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
    8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件
    9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件

  • 相关阅读:
    上传文件过大的问题FileUploadBase$SizeLimitExceededException
    Oracle分页2
    详解struts2中struts.properties
    Oracle 分页
    Xcode常见错误以及解决方案
    设置时间格式
    UIScrollView解决touchesBegan等方法不能触发的解方案
    ViewController 之间设置转场动画
    IQKeyboredManager使用
    SVN
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/10076697.html
Copyright © 2011-2022 走看看