ListofEquityとそのプロパティとしてのStringで構成されるEquityOrderクラスがあります。メソッド「placeOrder」も定義されています。コンストラクターを自動配線し、EquityのBeanがXMLファイルで定義されました。
@Autowired
public EquityOrder(String name, List<Equity> equity) {
this.name = name;
this.equity = equity;
}
public void placeOrder() {
for (Equity eq : equity)
System.out.println("Placing order for " + eq.getSecurityName()
+ "with quantity " + eq.getQuantity());
}
xmlには、エラーのない以下のシナリオがあります。
<context:annotation-config />
<bean name="testEquity" class="com.sapient.Spring.Equity.Equity">
<property name="symbol" value="MSFT"></property>
<property name="securityName" value="Microsoft"></property>
<property name="type" value="IT"></property>
<property name="quantity" value="100"></property>
</bean>
<bean name="testOtherEquity" class="com.sapient.Spring.Equity.Equity">
<constructor-arg value="GOOG">
</constructor-arg>
<constructor-arg value="Google"></constructor-arg>
<constructor-arg value="IT"></constructor-arg>
<constructor-arg value="100"></constructor-arg>
</bean>
<bean name="testEquityOrder" class="com.sapient.Spring.Equity.EquityOrder">
</bean>
問題-コンストラクターに文字列がない場合、プロジェクトは正常に実行されます。ただし、コンストラクターにStringが含まれている場合は、-として例外がスローされます。
Error creating bean with name 'testEquityOrder' defined in class path resource
[SpringConfig.xml]: Unsatisfied dependency expressed through constructor argument with
index 0 of type [java.lang.String]: : No matching bean of type [java.lang.String] found
for dependency: expected at least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations: {}; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of
type [java.lang.String] found for dependency: expected at least 1 bean which qualifies
as autowire candidate for this dependency. Dependency annotations: {}
これは、コンテナが文字列Beanを探しているが、そこに存在できないことを意味します。この問題を解決する方法。1つの方法は、セッターで注釈を使用することです。しかし@Autowire
、コンストラクターのアノテーションを使用してそれを行う方法は?ありがとう