0
XMLベースのSpringコンフィグレーションBeanでファクトリメソッドを継承することができます。
コントローラインタフェース:Spring XML設定でのファクトリメソッドBean継承
public interface Controller {
String method();
}
ControllerFactoryクラス:
public class ControllerFactory {
public Controller getController(String controllerName){
switch(controllerName){
case "OtherController":
return new OtherController();
case "SampleController":
return new SampleController();
default:
throw new IllegalArgumentException("Wrong controller name.");
}
}
}
SampleController実装:
public class SampleController implements Controller {
@Override
public String method() {
return "SampleController";
}
}
OtherController実装:
私はそれを実装しようとしましたpublic class OtherController implements Controller {
@Override
public String method() {
return "OtherController";
}
}
しかし、次のXML構成:
<!--factory method inheritance -->
<bean id="controllerFactory" class="factory.ControllerFactory"/>
<bean id="parentController" abstract="true" factory-bean="controllerFactory" factory-method="getController"/>
<bean id="otherController" parent="parentController">
<constructor-arg index="0" value="OtherController"/>
</bean>
は、コンパイル時にエラーを与える:
No matching constructor found in class 'Controller'
は、どのように私はそれが適切に実装ファクトリメソッド豆の継承を持つように変更できますか?予想通り、子ビーン作品へ
コピー工場法の構成:次のようにID parentController
と
<bean id="otherController" parent="parentController" factory-bean="controllerFactory" factory-method="getController">
<constructor-arg index="0" value="OtherController"/>
</bean>
constructor-argも渡します。 – SachinSarawgi