【发布时间】:2020-12-15 06:30:41
【问题描述】:
如果我有以下两个类 我们可以看到这是 SomeBean 的构造函数注入,但是 SomeBean 中的构造函数只有带有参数“String words”的构造函数,那么在依赖注入过程中,我们如何为依赖的构造函数指定参数?
@Component
public class SomeBean {
private String words;
public SomeBean(String words) {
this.words = words;
}
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
public void sayHello(){
System.out.println(this.words);
}
}
@Service
public class MapService {
private SomeBean someBean;
@Autowired
public MapService(SomeBean someBean) {
this.someBean = someBean;
}
public MapService() {
}
public void sayHello(){
this.someBean.sayHello();
}
}
【问题讨论】:
-
您可能需要使用
@Bean方法来定义您的SomeBean对象。
标签: java spring dependency-injection