我正在尝试为我的春豆创建工厂类
public interface MyBean implements TBean{}
@Component
@Scope("prototype")
public class MyBeanImpl implements MyBean{
public MyBeanImpl(Request request){//..}
}
@Component
public class MyFactory {
public <T extends TBean> T createbean(Class<T> interfaceToCreate, Object... args) {
return (T)AppContext.getApplicationContext().getBean(interfaceToCreate, args);
}
}
方法AppContext.getApplicationContext()
返回ApplicationContext对象
这里我应该能够创建一个像bean一样的弹簧bean:
@Autowired
protected MyFactory factory;
Request myRequest;
void somemethod(){
factory.createbean(MyBean.class, myRequest)
}
但是BeanFactory没有公开任何方法,例如:getBean(Class<T> clazz, Object... args)
我没有MyBeanImpl
类
有谁知道如何实现这一目标?
答案 0 :(得分:1)
您可以分两步完成。 ListableBeanFactory
提供了方法String[] getBeanNamesForType(Class<?> type)
。获得类型的bean名称后,可以调用BeanFactory.getBean(String name, Object... args)
。