【发布时间】:2012-10-23 20:11:46
【问题描述】:
我试图为我的 spring bean 创建工厂类
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对象
在这里我应该可以创建一个像这样的spring bean:
@Autowired
protected MyFactory factory;
Request myRequest;
void somemethod(){
factory.createbean(MyBean.class, myRequest)
}
但BeanFactory 没有公开任何方法,例如:getBean(Class<T> clazz, Object... args) 而且我没有MyBeanImpl 类的默认构造函数
有谁知道如何做到这一点?
【问题讨论】:
标签: spring