定义一个javaBean
public class User {
public String name;
public String age;
public User(String name, String age) {
this.name = name;
this.age = age;
}
}
正常赋值写法
给javaBean添加get/set方法:
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
赋值:
@Configuration
public class MyConfig {
@Bean
public User user() {
User user = new User("张三","18");
System.out.println(user);
user.setName("李四");
user.setAge("17");
System.out.println(user);
return user;
}
}
链式写法
修改get/set方法:
public String getName() {
return name;
}
public User setName(String name) {
this.name = name;
return this;
}
public String getAge() {
return age;
}
public User setAge(String age) {
this.age = age;
return this;
}
赋值:
@Configuration
public class MyConfig {
@Bean
public User user() {
User user = new User("张三","18");
System.out.println(user);
user.setName("李四").setAge("17");
System.out.println(user);
return user;
}
}
idea快捷生成链式set方法
在快捷生成弹窗内选择模板为Builder即可: