我有一个类型为FooProcess
(接口)[空对象]的对象'Foo Action'。 我想初始化'Foo Action'作为FooProcess
的一个子类的对象[FooOne
或FooTwo
]。如何使用Spring Framework从类路径/名称创建对象?
使用Spring Framework,我能够创造ArrayList
FooList
(中FooProcess
子类的名称列表),现在我想初始化FooAction
的一个子类。 (给定参数selected
定义,我希望它作为初始化哪个类)
的FooProcess
所有子类都具有接受String
构造。
我的问题特别是在这条线
FooAction = component.getClass().getConstructor(f);
完整的方法:
public FooProcess Load(String selected, String f) throws ClassCastException, InstantiationException, IllegalAccessException, ClassNotFoundException{
ArrayList<String> FooList = new ArrayList<String>();
final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.addIncludeFilter(new AssignableTypeFilter(FooProcess.class));
for (BeanDefinition component : provider.findCandidateComponents("org.project.foomodule.systems")) {
if(selected == component.getBeanClassName()){
FooAction = component.getClass().getConstructor(f);
} }
return FooAction;
}
这是什么问题? – skaffman 2012-01-15 12:56:57
为什么不使用Spring Bean Factory来实例化对象并注入依赖关系? – duffymo 2012-01-15 12:58:30
@skaffman我在帖子中暗示的'问题'并不能满足我需要,而且IDE说它在语法上是错误的。 – JD009 2012-01-15 12:58:56