Spring Boot 学习 (五)@ConfigurationProperties 和 @Value 自定义参数绑定

大家在开发过程中,有时可能会遇到这样的场景,我们想要读取我们的配置文件信息,读取并封装到一个实体类中,这样我们在代码中使用配置参数就相对方便了很多。SpringBoot 为我们提供的注解 @ConfigurationProperties 和 @Value,便可以很方便的帮我们实现配置参数的绑定功能。那么接下来我们将介绍一下这两者怎么使用,以及有什么区别:

一、@ConfigurationProperties 注解方式

1)自定义配置类

如果我们所要绑定的类是我们自定义的,我们可以使用以下方法绑定:

a. 首先我们的配置文件application.properties中的配置信息如下:

myproperties.userName = LINCO
myproperties.password = 123456

b.接下来我们编写一个自定义的配置类MyProperties.java , 在自定义配置类中加上如下注解:

@ConfigurationProperties(prefix = "myproperties")
#这里的prefix是我们在application.properties中定义的前缀
#SpringBoot的@ConfigurationProperties已经为我们设置了默认值prefix
#所以这里也可以直接省略为@ConfigurationProperties("myproperties")
package com.colinlin.hellospringboot.hellodemo;

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

/**
 * 自定义配置类
 *
 * @author Colin Lin
 * @create 2018-09-09 10:47
 **/
@Component
@ConfigurationProperties(prefix = "myproperties")
public class MyProperties {

    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

c.我们写一个Controller测试一下我们的绑定结果:

package com.colinlin.hellospringboot.hellodemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @Autowired MyProperties myProperties;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){

        return "UserName: " + myProperties.getUserName() + " Password: " + myProperties.getPassword();
    }
}

d.启动我们的程序,并且访问我们的的controller:localhost:8080/hello,可以得到以下结果与我们配置文件中的信息一致:

UserName: LINCO Password: 123456

2)第三方提供的组件类

上面的绑定方法只针对我们自定的类才可以这样做,当我们想要绑定的类是第三方提供的,我们无法直接在类中加注解,此时我们可以用下面的方法实现参数绑定(当然以下方法也适用于自定义类的参数绑定): 

把@ConfigurationProperties直接定义在@bean的注解上,这时我们先假设自定义的实体类MyProperties是第三方提供的(注意:此时在MyProperties类中不需要@Component和@ConfigurationProperties了)

package com.colinlin.hellospringboot;

import com.colinlin.hellospringboot.hellodemo.MyProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class HellospringbootApplication {

    /*第三方组件类绑定参数,也适用于自定义类*/
    @Bean
    @ConfigurationProperties("myproperties")
    public MyProperties getMyProperties(){

        return new MyProperties();
    }
    /*第三方组件类绑定参数,也适用于自定义类*/
    
    public static void main(String[] args) {
        SpringApplication.run(HellospringbootApplication.class,args);
    }
}

此时我们重新启动程序,访问localhost:8080/hello可以看到与第一种方法一样都可以拿到我们的的配置信息,这里不再重复阐述。

二、@Value 注解方式

@Value 注解方式需要一个一个属性对应使用@Value("${propertiesName}")指定。我们仍旧使用上面的MyProperties类进行演示,代码修改如下:

package com.colinlin.hellospringboot.hellodemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 自定义配置类
 *
 * @author Colin Lin
 * @create 2018-09-09 10:47
 **/
@Component
public class MyProperties {

    @Value("${myproperties.userName}")
    private String userName;
    @Value("${myproperties.password}")
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

此时运行我们的程序,访问localhost:8080/hello依然可以拿到我们的配置参数值。

三、@ConfigurationProperties @Value 的区别

 @ConfigurationProperties@Value
注解功能可以批量注入配置文件中的属性只能一个个指定注入属性
松散语法绑定(Relaxed binding)支持不支持
EL表达式不支持支持
JSR303数据校验 @Validated支持不支持
复杂类型封装支持不支持

附:

所谓松散语法也就是属性命名规则(Relaxed binding)

- person.firstName:使用标准方式
- person.first-name:大写用-         
- person.first_name:大写用_
- PERSON_FIRST_NAME: 系统属性推荐使用这种写法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值