目录

 

理论

例子

解决properties文件编码


 

理论

@ConfigurationProperties注解是把Person加入到容器中,

<bean>标签也是加入到容器中:

<bean class="Person">
    <property name="lastName" vale="字面量/${key}从环境变量、配置文件中获取值/#{spEL}"></property>
<bean/>

@Value和@ConfigurationProperties为属性注入值对比

 @ConfigurationProperties@Value
功能批量注入配置文件中的属性一一指定
松散绑定(松散语法)支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持

 

配置文件yml还是properties他们都能获取得到值;

 

@ConfigurationProperties

与@Bean结合为属性的赋值

与@PropertySource(只能用于properties文件)结合读取指定文件

 

属性名匹配规则(Relaxed binding)

标准写法:person.firstName

大写用-:person.first-name

大写用_:person.first_name

推荐属性:PERSON_FIRST_NAME

 

例子

项目结构如下:

源码如下:

Dog.java

package com.peoperties.peoperties.bean;

public class Dog {

    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + 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;
    }
}

Person.java 

package com.peoperties.peoperties.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

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

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

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

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

    //@Value("true")
    private Boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", 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 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;
    }
}

PeopertiesApplication.java

package com.peoperties.peoperties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PeopertiesApplication {

    public static void main(String[] args) {
        SpringApplication.run(PeopertiesApplication.class, args);
    }

}

application.properties

server.port=8081

#配置person的值
person.last-name=李四
person.age=18
person.birth=2019/3/4
person.boss=false
person.maps.k1=v1
person.maps.k2=19
person.lists=a,b,c,d,e,f,g,h
person.dog.name=小白
person.dog.age=15

PeopertiesApplicationTests.java

package com.peoperties.peoperties;

import com.peoperties.peoperties.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PeopertiesApplicationTests {

    @Autowired
    Person person;

    @Test
    public void contextLoads() {

        System.out.println(person);
    }

}

运行截图如下:

 

 

解决properties文件编码

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐