@Component
class Person{
private Address add;
@Autowired
public Person(Address add) { <--- An instance of Address will be injected here
this.add = add;
//this.add.setStreet("Custom Street"); <--- you can modify that here before the Autowiring constructor ends
/* if (add.getStreet().isEmpty()) { <--- you can do whatever you want inside the constructor
add.setStreet("Should not be empty") <--- you can check conditionally and set everything you want
} */
}
}
@Component
class Address{
String street;
}
2条答案
按热度按时间um6iljoc1#
我们可以在spring表达式中设置默认值
{表达式?:默认值}
s4n0splo2#
您可以在构造函数上使用自动关联并修改person的行为。
你的面试官可能希望你能解释一下