欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot通过配置文件值获取

程序员文章站 2022-03-08 17:55:10
...

IDEA中创建SpringBoot项目
选择Spring Web模块

整体构架

SpringBoot通过配置文件值获取

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.uek</groupId>
    <artifactId>springboot02-study02-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot02-study02-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

bean

public class Dog {

    private String name;
    private Integer age;

    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;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
 *   prefix = "persopn":配置文件中哪个下面的所有属性进行一一映射
 *
 * ConfigurationProperties(prefix = "person"):支持检验      默认是从全局配置文件中获取值
 * @Value("${person.last-name}"):不支持校验
 *
 * @PropertySource(value = {"classpath:person.properties"}):可以读取指定的配置文件
 */
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated   //校验
public class Person {

    //@Email
    //@Value("${person.last-name}")
    private String lastName;

    //@Value("#{11*2}")
    private Integer age;

    //@Value("true")
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> maps;
    private List<Object>lists;
    private Dog dog;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    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 getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    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='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

config


/**
 * @Configuration:指明当前类是一个配置类,就是来替代之前的spring配置
 */
@Configuration
public class MyAppConfig {

    //使用@Bean给容器中添加组件
    @Bean
    public HelloService helloService(){
        System.out.println("配置类@bean给容器中添加组件了...");
        return new HelloService();
    }

}

controller

@RestController
public class HelloController {

    //获取配置文件中的值
    @Value("${person.last-name}")
    private String name;

    @RequestMapping("/hello")
    public String hello(){
        return "hello"+name;
    }
}

seervice

public class HelloService {
}

通过properties文件值获取

# 配置Person的值�ֵ
#person.last-name=张三
#person.age=18
#person.birthday=2020/02/02
#person.boss=false
#person.maps.k1=v1
#person.maps.k2=14
#person.lists=a,b,c
#person.dog.name=小黄
#person.dog.age=15

通过yml文件值获取

# 配置Person的值
#person:
#   lastName: 张三
#   age: 18
#   boss: false
#   birthday: 2020/02/02
#   maps: {k1: v1,k2: 12}
#   lists:
#     - 李四
#     - 王五
#   dog:
#     name: 小狗
#     age: 2

指定的配置文件值获取

person.properties

person.last-name=王五
person.age=18
person.birthday=2020/02/02
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=小黄
person.dog.age=15