我正在學習spring框架,並有一個非常基本的問題。我試圖找到答案,但找不到它,請耐心等待。春天我已經看到了下面這種接線方式(就是它的名字)。Spring中的構造函數注入。它不再是一個bean嗎?
public class A {
private B b;
public A(B b) {
this.b = b;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
public class B {
private String foo;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
所以我明白,這種自動裝配是使用構造函數注入完成的。然後在context.xml
我有以下
<bean id="a" class="A" autowire="constructor">
</bean>
<bean id="b" class="B" >
<property name="foo" value="foo1" />
</bean>
(我用的註解,而不是context.xml
學習的配置,但因爲它似乎提供了更清晰的畫面在這裏用它)。所以我的問題是,因爲根據定義,bean應該只有無參數構造函數和getter和setter,不會進行構造函數注入,從而使其不能成爲bean?我在這裏錯過了什麼明顯的東西?
Bean配置不應該在web.xml中。它應該是在春季配置xml – Keerthivasan
@Keerthivasan抱歉,那是我的錯誤。我會糾正它。 – mahacoder
請查看此鏈接http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-properties-detailed – Keerthivasan