SptringBoot第三讲(SpringBoot配置文件之properties application. properties)

在Spring Boot中已经给创建好的application.properties

中创建如下:

server.port=8081

#idea使用的是utf-8,在Setting----File Encodings-----utf-8 后边打上对号
#配置person的值
person.lastName=张三
person.age=18
person.boss=false
person.birth=2017/12/12
person.maps.k1=v1
person.maps.k2=v2
person.lists=lisi,zhaoliu
person.dog.name=小狗
person.dog.age=2

javaBean代码如下

package com.atguigu.springboot.bean;

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

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

/*
* 将配置文件的每一个属性的值,银蛇到这个组件中
* @ConfigurationProperties:告诉SpringBoot降本类中的所有属性和配置文件中相关的配置进行绑定
*       prefix = "person":配置文件中那个下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才嫩给容器提供的@ConfigurationProperties功能
*
* */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {


    private String  lastName;
    private Integer age;
    private Boolean boss;
    private Date    birth;


    private Map maps;
    private List 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 getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List 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 +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
 
  

测试执行代码如下

package com.atguigu.springboot;

import com.atguigu.springboot.bean.Person;
import org.junit.AfterClass;
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;


/*
* SpringBoot单元测试
*
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
* */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {

    @Autowired
    Person person;

    @Test
    public void contextLoads() {

        System.out.print(person);

    }

}

还可以有第二种写法,但是第一种写法 可以批量注入,代码如下:

package com.atguigu.springboot.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;

/*
* 将配置文件的每一个属性的值,银蛇到这个组件中
* @ConfigurationProperties:告诉SpringBoot降本类中的所有属性和配置文件中相关的配置进行绑定
*       prefix = "person":配置文件中那个下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才嫩给容器提供的@ConfigurationProperties功能
*
* */
@Component
//@ConfigurationProperties(prefix = "person")
@vealidated
public class Person {

    /*
    *
    * 
    *
    *     
    *
    * 
    * */


    @Value("${person.lastName}")
    private String  lastName;
    @Value("#{11*7}")
    private Integer age;
    private Boolean boss;
    private Date    birth;


    private Map maps;
    private List 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 getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List 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 +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
 
  

 

@Value获取值和@ConfigurationProperties获取值比较

  @ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个一个指定
松散绑定(松散绑定语法lastNme,last-name是一样的) 支持 不支持
SpEL(Spring表达式) 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装(如map) 支持 不支持

配置文件注入值数据校验 :JSR303数据校验如下:

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

    /*
     *JSR303 定义lastName输入的格式是Email格式
     */
    @Email
    private String  lastName;
    private Integer age;
    private Boolean boss;
    private Date    birth;

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

什么时候用@Value和@ConfigurationProperties?

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value

package com.atguigu.springboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

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

    @RequestMapping("/sayhello")
    public String sayHello(){
        return "Hello "+name;
    }
}

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties

 

 

 

 

 

你可能感兴趣的:(SpringBoot)