码迷,mamicode.com
首页 > 其他好文 > 详细

SpingBoot之配置文件的值注入问题

时间:2018-10-11 22:37:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:映射   ann   err   必须   loser   容器   actor   哪些   -name   

我们在这里研究的是以yml配置文件值注入的问题:

Person:
    lastName: 张三
    age: 23
    boss: false
    birth: 2018-10-11
    maps: {k1: v1,k2: v2}
    lists:
      - pig
      - dog
    dog:
      name: 半半
      age: 1

定义了一个Person的实体类:

package com.example.demo.entity;
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<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 +
                ‘}‘;
    }

再定义一个dog的实体类

package com.example.demo.entity;
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;
    }
}
set/get方法......

再定义一个测试类:

package com.example.demo;
import com.example.demo.entity.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;
/**
 * springBoot测试
 * 可以在测试期间可以很方便的类似编码一样进行自动注入等容器的功能
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    @Autowired
    Person person;
    @Test
    public void contextLoads() {
        System.out.println(person);
    }
}

测试结果:

技术分享图片

重点:

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

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

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

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

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

所以我们也可以利用@Value()进行赋值:

@Componentpublic class Person {
   //lastName必须是邮箱格式@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;

在配置文件值注入的时候进行数据校验:

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
   //lastName必须是邮箱格式
    @Email
    private String lastName;
    private Integer age;private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
private Boolean boss;

3、@PropertySource&@ImportResource&@Bean

@PropertySource:加载指定的配置文件,将配置文件中配置的每一个属性的值,映射到这个组件中
@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定,
prefix = "person":配置文件中哪个下面的所有属性进行一一映射只有这个组件是容器中的组件,才能

容器提供的@ConfigurationProperties功能,@ConfigurationProperties(prefix = "person")默认从全局

配置文件中获取值。

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}
set/get方法.....

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式

1、配置类@Configuration

2、使用@Bean给容器中添加组件

@Configuration
public class MyAppConfig {
    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloServicfun(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }
}

 

SpingBoot之配置文件的值注入问题

标签:映射   ann   err   必须   loser   容器   actor   哪些   -name   

原文地址:https://www.cnblogs.com/xyzmy/p/9775387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!