Spring XML 配置中的工厂方法 bean 继承
Factory method bean inheritance in Spring XML configuration
我读到,基于XML的Spring配置bean可以继承工厂方法。
我尝试实现它:
控制器接口:
public interface Controller {
String method();
}
控制器工厂class:
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.");
}
}
}
示例控制器实现:
public class SampleController implements Controller {
@Override
public String method() {
return "SampleController";
}
}
其他控制器实现:
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'
如何更改它以正确实现工厂方法 bean 继承?
将工厂方法配置复制到子 bean 按预期工作:
<bean id="otherController" parent="parentController" factory-bean="controllerFactory" factory-method="getController">
<constructor-arg index="0" value="OtherController"/>
</bean>
更改 ID 为 parentController
的 bean,如下所示:
<bean id="parentController" class="factory.ControllerFactory" factory-bean="controllerFactory" factory-method="getController">
<constructor-arg index="0" value="OtherController"/>
</bean>
.
试试这个可能有用。
我读到,基于XML的Spring配置bean可以继承工厂方法。
我尝试实现它:
控制器接口:
public interface Controller {
String method();
}
控制器工厂class:
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.");
}
}
}
示例控制器实现:
public class SampleController implements Controller {
@Override
public String method() {
return "SampleController";
}
}
其他控制器实现:
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'
如何更改它以正确实现工厂方法 bean 继承?
将工厂方法配置复制到子 bean 按预期工作:
<bean id="otherController" parent="parentController" factory-bean="controllerFactory" factory-method="getController">
<constructor-arg index="0" value="OtherController"/>
</bean>
更改 ID 为 parentController
的 bean,如下所示:
<bean id="parentController" class="factory.ControllerFactory" factory-bean="controllerFactory" factory-method="getController">
<constructor-arg index="0" value="OtherController"/>
</bean>
.
试试这个可能有用。