当前位置 博文首页 > 逐墨飞扬的博客:SpringBoot配置文件

    逐墨飞扬的博客:SpringBoot配置文件

    作者:[db:作者] 时间:2021-07-12 15:46

    配置文件加载

    SpringBoot默认会从以下几个位置加载配置文件application.properties(或application.yml):

    1. A /config subdirectory of the current directory
    2. The current directory
    3. A classpath /config package
    4. The classpath root

    如果配置文件的名称不是默认的,可以通过spring.config.name属性指定配置名称

    如果配置文件的路径不是默认路径,可以通过spring.config.location属性指定

    profile

    参见:Profile多环境运行

    支持的属性清单

    参见:官方配置属性清单

    配合maven使用占位符

    在构建时自动展开属性,可以使用现有的构建配置自动扩展它们,而不是硬编码在项目的构建配置中指定的某些属性。
    您可以使用资源过滤从Maven项目自动扩展属性。如果您使用spring-boot-starter-parent,则可以使用@..@占位符引用Maven“项目属性” ,如以下示例所示:

    profile多环境运行

    spring.profiles.active=@profileName@
    

    如果不使用启动父节点,您需要在pom.xml的元素中包含以下元素:

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 
                	这里可以定义多个参数,标签的名字就对应properties文件中的key
                    比如:<profileName>在配置文件中写成@profileName@
                 -->
                <profileName>dev</profileName>
            </properties>
        </profile>
    </profiles>
    

    您还需要在中包含以下元素:

    <!-- 
    	maven resource插件:
        功能:主要是操作配置文件相关。
        比如用maven中的变量替换项目中配置文件中的占位符
        显示指定非maven标准resources目录为resources
     -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <configuration>
            <!-- 指定配置文件中的占位符是@ -->
            <delimiters>
                <delimiter>@</delimiter>
            </delimiters>
            <useDefaultDelimiters>false</useDefaultDelimiters>
        </configuration>
    </plugin>
    

    类型安全的Properties读取

    当系统中参数值很多时,我们往往会将配置参数分门别类,然后通过@ConfigurationProperties注解直接将读取到的值封装到一个JavaBean上

    示例:

    在src/main/resources下新建config.properties

    demo.phone=10086
    demo.wife=self
    

    创建ConfigBeanProp类并注入config.properties中的值:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "demo")
    @PropertySource(value = "config.properties")
    public class ConfigBeanProp {
    
        private String phone;
    
        private String wife;
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    }
    

    @Value注解读取application.properties参数

    配置文件

    application.properties

    com.name="张三"
    com.age=23
    
    bean类

    People.java

    /**
     * 读取application.properties中的自定义属性
     */
    @Component
    public class People {
        @Value("${com.name}")
        private String name;
        @Value("${com.age}")
        private int age;
    
    	public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    测试类

    TestRead.java

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestRead {
        @Autowired
        private People people;
    
        @Test
        public void testReadConf() {
    		System.out.println("姓名:" + people.getName());
    		System.out.println("年龄:" + people.getAge());
        }
    }
    
    cs