SpringBoot的@ConfigurationProperties与@Value以及修改自动配置源码分析

SpringBoot的@ConfigurationProperties@Value都可以将全局配置文件的值赋给指定的属性

  1. @ConfigurationProperties支持批量注入,该注解定义在类上,可以设置prefix属性指定前缀
    如:

    @Component
    @ConfigurationProperties(prefix = "test")
    public class Student {
    
        private String name;
        private Integer age;
        private String sex;
    
    
        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 String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        @Override
        public String  toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    

    application.properties文件:

    test.name=fei
    test.age=20
    test.sex=man
    

    测试可以发现,Student的同名属性成功赋上了值
    测试代码

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class FirstspringbootApplicationTests {
    
    	@Autowired
    	private Student student;
    
    	@Test
    	public void contextLoads() {
    		System.out.println(student);
    	}
    }
    

    在这里插入图片描述

  2. @Value常常用于给单个属性赋值,支持SPEL(spring表达式),注解直接用于属性上
    如:

    @Component
    public class Student {
    
        @Value("${test.name}")
        private String name;
    
        @Value("${test.age}")
        private Integer age;
    
        @Value("${test.sex}")
        private String sex;
    
        @Override
        public String  toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }
    

    SPEL:spring表达式就是以${}这种方式取值,或者以#{2*8}这种形式计算表达式

    最后测试结果是一致的
    在这里插入图片描述

  3. @Value使用@Value("${test.name}")这种方式取值,要是在配置文件里面没有定义test.name,那启动会报错。而@ConfigurationProperties没有同名属性也不会报错。

  4. @ConfigurationProperties赋值必须需要该属性提供getset方法,而@Value则不需要

  5. 补充:

    修改SpringBoot的默认配置就是通过`@ConfigurationProperties映射回去的

    比如 http编码自动配置
    HttpEncodingAutoConfiguration.class提供的配置类HttpProperties.class
    在这里插入图片描述

    HttpProperties.class类上就提供了
    @ConfigurationProperties(prefix = "spring.http")注解
    继续看,在HttpProperties.class类中定义了一个静态内部类
    public static class Encoding
    Encoding 这个类有一个charset属性
    在这里插入图片描述在这里插入图片描述
    可以看到默认编码是UTF-8
    那根据我们之前学的知识知道,我们可以在全局配置文件里面修改默认的http编码
    使用方法就是

    spring.http.Encoding.charset=xxxx
    
    因为启动的时候@ConfigurationProperties注解会为相应属性注入值
    

    修改其他默认属性,原理大同小异

    使用的版本2.1.4.RELEASE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值