java框架之Spring boot三:SpringBoot自定义配置一

版权声明:本文为博主原创文章,欢迎转载并标明出处。 https://blog.csdn.net/u014785563/article/details/79954213

java框架之Spring boot三:SpringBoot自定义配置一

私有化配置文件

刚才我们介绍了在主配置文件汇中配置对应的文件,如果我们想要自定义配置文件该怎么处理呢?
现在就要给大家介绍我们的@PropertySource注解。
@PropertySource:注解可以从properties文件中,获取对应的key-value值,将其赋予变量
同时,这个注解可以加载多个配置文件生成对应的bean类。
对应的bean,Person.java:

package com.example.demo.bean;


import java.util.Date;
import java.util.Map;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@PropertySource(value= {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix="person")
public class Person {
    /**/
    private String name;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
    public Person() {
        super();
    }
    public Person(String name, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> lists,
            Dog dog) {
        super();
        this.name = name;
        this.age = age;
        this.boss = boss;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Boolean getBoss() {
        return boss;
    }
    public void setBoss(Boolean boss) {
        this.boss = boss;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, Object> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }
    public List<Object> getLists() {
        return lists;
    }
    public void setLists(List<Object> lists) {
        this.lists = lists;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "Person [lastName=" + name + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
                + maps + ", lists=" + lists + ", dog=" + dog + "]";
    }

}

对应的配置文件,person.properties:

person.name=bbbb
person.age=21
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=15
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

其他不变,这样就能获取到该配置文件中的信息了

注意事项:
1、@ConfigurationProperties(prefix=”person”)这个注解不能取消
2、person.properties的路径要正确

配置文件占位符

配置文件的书写:

person.name=bbbb${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=15
person.lists=a,b,c
person.dog.name=${person.name}'s dog
person.dog.age=15

实现的结果:

Person [lastName=bbbb36f62437-bac7-4c8e-8686-c7c9b02f3d92, age=1353596658, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=15}, lists=[a, b, c], dog=Dog [name=bbbbbeca97d0-7c5e-4e96-abe3-a8d630672275's dog, age=15]]

profile

profile是Spring对不同环境提供不同配置的功能的支持,可以通过激活、指定参数等方式快速切换环境变量。
主要说一下两种方式:

多profile文件

我们在主配置文件编写的时候,文件名可以使 application-{profile}.properties

application.properties:

server.port=8081
spring.profiles.active=dev

application-dev.properties:

server.port=8082

注意:当有多个配置文件后,没有进行配置文件配置,默认启用application.properties

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页