前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 构造器参数绑定,越来越强大了!

Spring Boot 构造器参数绑定,越来越强大了!

作者头像
Java技术栈
发布2019-12-02 13:19:10
8360
发布2019-12-02 13:19:10
举报
文章被收录于专栏:Java技术栈Java技术栈

前几天,Spring Boot 2.2.0 正式发布了:Spring Boot 2.2.0 正式发布,支持 JDK 13!,文中有提到基于构造器的参数绑定,那么今天栈长就带大家来实践一下,到底怎么用,有什么用。

废话不说,先上示例代码:

代码语言:javascript
复制
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;

/**
 * 微信公众号:Java技术栈
 */
@ConstructorBinding
@ConfigurationProperties(prefix = "tom")
public class TomProperties {

    private String name;
    private String sex;
    private int age;
    private String country;
    private Date entryTime;

    public TomProperties(String name,
                         String sex,
                         int age,
                         @DefaultValue("China") String country,
                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date entryTime) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.country = country;
        this.entryTime = entryTime;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public int getAge() {
        return age;
    }

    public String getCountry() {
        return country;
    }

    public Date getEntryTime() {
        return entryTime;
    }

    @Override
    public String toString() {
        return "TomProperties{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", entryTime=" + entryTime +
                '}';
    }

}

配置文件内容:

代码语言:javascript
复制
tom:
  name: Tom
  sex: man
  age: 18
  entry-time: 2012-12-12 12:00:00

参数结果输出:

TomProperties{name='Tom', sex='man', age=18, country='China', entryTime=Wed Dec 12 12:00:00 CST 2012}

通过构造器的参数绑定,其实就是在 @ConfigurationProperties 注解的基础上再添加一个 '@ConstructorBinding' 注解。

@ConstructorBinding几点总结:

1、用了 @ConstructorBinding 这个注解,就标识这个类的参数优先通过带参数的构造器注入,如果没有带参数的构造器则再通过 setters 注入;

怎么判断是通过 setters 注入还是构造器注入,请看这个类的源码:

org.springframework.boot.context.properties.ConfigurationPropertiesBean.BindMethod

2、当 @ConstructorBinding 用在类上时,该类只能有一个带参数的构造器;如果有多个构造器时,可以把 @ConstructorBinding 直接绑定到具体的构造方法上;

3、成员变量可以是 final 不可变;

4、支持该类的内部类构造器注入的形式;

5、支持默认值 @DefaultValue@DateTimeFormat 时间格式等注解配合使用;

6、需要配合 @ConfigurationProperties@EnableConfigurationProperties 注解使用;

7、不支持像 @Component@Bean@Import 等方式创建 bean 的构造器参数绑定;

来看下它的源码:

代码语言:javascript
复制
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConstructorBinding {
}

什么参数都没有,可以说明,它就是起到一个构造器参数绑定的标识作用。

涨姿势了吧??又学会了一种绑定参数的新方法了!

- END -

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java技术栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档