私はこのようなクラスを持っているとしましょう:
public class A {
private B b;
public A() {
}
// Some code calling methods in b.
}
Spring にパラメーター化されたコンストラクターを追加せずに、XML 構成を介して B のインスタンスを A に挿入するにはどうすればよいですか?
パラメータ化されたコンストラクタとセッターの両方を使用できますか?
public class A {
private B b;
public A(B b) {
this.b = b;
}
public void setB(B b) {
this.b = b;
}
// Some code calling methods in b.
}
編集:すべての回答に感謝します。私の実際の問題は、次のようなクラスがあることです:
公開クラスA {
private B b;
public A(B b) {
this.b = b;
}
// Some code calling methods in b.
}
下位互換性の理由から、パラメーター化されたコンストラクターを削除せずに、上記のクラスのデフォルトのコンストラクターが必要です。
そう 、
- Bean を追加すると、Bean はデフォルトのコンストラクターに自動的に注入されますか? または、XML ファイルに挿入する追加の構成が必要ですか?
- セッターを追加する場合、XML でどのような追加の構成変更を行う必要がありますか?
以下のようなxmlファイルがあるとします:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="name"
class="A">
<constructor-arg ref="B" />
</bean>
</beans>