@Configuration
@EnableConfigurationProperties(MyAutoConfiguration.class)
@ConfigurationProperties(prefix = "person")
public class MyAutoConfiguration {
private final MyProperties myProperties;
public MyAutoConfiguration(MyProperties myProperties) {
this.myProperties = myProperties;
}
//1.
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
//创建一个bean
@Bean(name = "myBeanWithAge")
public MyBean MyBeanWithAge(){
return new MyBean(this.age);
}
}
public class MyBean {
private String bean;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void setBean(String bean) {
this.bean = bean;
}
public String getBean() {
return bean;
}
public MyBean(String age) {
this.age = age;
System.out.println("构造函数含有一个参数,age="+age);
}
public MyBean(String bean,String age) {
this.bean = bean;
this.age=age;
System.out.println("初始化构造函数,有2参");
}
}
application.yml文件的配置
person:
name: zs
age: 12
sex: nv