In previous posts: Spring Boot reads configuration in several ways, I introduce Spring Boot based on Java Bean parameter binding, A Java Bean class is identified with the @ConfigurationProperties annotation (see “Java Technology Stack “for more Spring Boot tutorials).
Spring Boot 2.2.0 is now available with JDK 13 support. Constructor based parameter binding is mentioned in the article, so today’s stack length will take you to practice, exactly how to use, what is useful.
Without talking nonsense, first sample 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; /** * 微信 号 : Java 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 +
'} '; }}Copy the code
Configuration file contents:
tom:
name: Tom
sex: man
age: 18
entry-time: 2012-12-12 12:00:00
Copy the code
Parameter result output:
TomProperties{name=’Tom’, sex=’man’, age=18, country=’China’, entryTime=Wed Dec 12 12:00:00 CST 2012}
By binding parameters to the constructor, you are essentially adding a ‘@constructorBinding’ annotation to the @ConfigurationProperties annotation.
ConstructorBinding:
ConstructorBinding @constructorBinding indicates that the class’s parameters are injected first through the constructor, and then through setters if there are no constructors.
To determine whether to inject through setters or constructors, see the source of this class:
org.springframework.boot.context.properties.ConfigurationPropertiesBean.BindMethod
2. When @ConstructorBinding is used on a class, the class can have only one constructor with an argument; If you have more than one constructor, you can bind @constructorBinding directly to the specific constructor.
3. Member variables can be final and immutable;
Support internal class constructor injection form for the class;
5, support the DefaultValue @defaultvalue, @datetimeformat time format and other annotations.
6, need to cooperate with @ ConfigurationProperties, @ EnableConfigurationProperties annotations use;
Constructor parameter bindings are not supported for @Component, @bean, @import, etc.
Take a look at its source code:
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConstructorBinding {
}
Copy the code
It has no arguments, as you can see, but serves as an identifier for the constructor parameter binding.
Did you get a lift? Learned a new way to bind parameters!
To obtain all Spring Boot sample code, please follow the wechat public account “Java Technology Stack “in the background reply keyword: bootcode.
In the end, the stack manager will share the latest technology tutorial of Spring Boot. Now he has written a pile of inventory, follow the wechat public account “Java Technology stack “, the public account will be pushed as soon as possible!