I was trying to create factory class for my spring beans
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);
}
}
method AppContext.getApplicationContext() returns ApplicationContext object
here I should be be able to create a spring bean like:
@Autowired
protected MyFactory factory;
Request myRequest;
void somemethod(){
factory.createbean(MyBean.class, myRequest)
}
but BeanFactory does not expose any method like: getBean(Class<T> clazz, Object... args) And I do not have default constructor for MyBeanImpl class
Does anyone know how to accomplish that?
You can achieve that in two steps. The
ListableBeanFactoryprovides a methodString[] getBeanNamesForType(Class<?> type). Once you got the bean names for your type, you can invoke theBeanFactory.getBean(String name, Object... args).