Spring Boot constructor parameter binding, more and more powerful!

Keywords: Java Spring JDK

In previous articles: Several ways for Spring Boot to read configuration , I introduced Java Bean-based parameter bindings in Spring Boot, identified on a Java Bean class with the @ConfigurationProperties annotation (more Spring Boot tutorials focus on the public number Java Technology Stack in the background reply: boot).

A few days ago, Spring Boot 2.2.0 was officially released: Spring Boot 2.2.0 officially released, supporting JDK 13! In this paper, parameter binding based on constructor is mentioned, so today the stack leader will take you to practice how to use it and what is the use.

Say nothing, let's start with the example code:

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;

/**
 * WeChat Public Number: Java Technology Stack
 */
@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 +
                '}';
    }

}

Profile content:

tom:
  name: Tom
  sex: man
  age: 18
  entry-time: 2012-12-12 12:00:00

Parameter result output:

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

By parametric binding to the constructor, you are adding an'@ConstructorBinding'comment to the @ConfigurationProperties comment.

Some summaries of @ConstructorBinding:

1. With the @ConstructorBinding annotation, the parameters identifying this class are injected first through the parameterized constructor, and then through setters if there is no parameterized constructor;

See the source code for this class to determine whether it is injected through setters or constructors:

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

2. When @ConstructorBinding is used on a class, it can only have one constructor with parameters; if there are more than one constructor, you can bind @ConstructorBinding directly to a specific construction method;

3. Member variables can be final immutable;

4. Forms that support injections of internal class constructors of the class;

5. Supports the use of annotations such as default value @DefaultValue, @DateTimeFormat time format, etc.

6. Need to be used with the @ConfigurationProperties, @EnableConfigurationProperties annotations;

7. Constructor parameter bindings such as @Component, @bean, @Import to create beans are not supported;

Take a look at its source code:

@Target({ElementType.TYPE, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConstructorBinding {
}

There are no parameters, which can be explained as identifying a constructor parameter binding.

Raise your posture?You've learned a new way to bind parameters!

Get all the Spring Boot sample code, and pay attention to the WeChat public number "Java Technology Stack" in the background reply keyword: bootcode.

In the end, the stack leader will share the latest Spring Boot technology tutorials one after another. Now he has written a stack of stocks. Focus on the WeChat public number "Java technology stack", and the public number will be launched for the first time!

Posted by MHardeman25 on Thu, 28 Nov 2019 23:06:58 -0800