I have a set of instances of FactoryBean which are of type MyFactoryBean. The constructor takes a Class<?> and uses that to determine what to return from getObject():
public class MyFactoryBean implements FactoryBean {
private final Class<?> type;
public MyFactoryBean(Class<?> type) {
this.type = type;
}
@Override
public Object getObject() throws Exception {
return null; // Obviously I return something here rather than null!
}
@Override
public Class<?> getObjectType() {
return type;
}
@Override
public boolean isSingleton() {
return false;
}
}
My end result is to control what is returned when an instance of type passed to the constructor of the MyFactoryBean is requested from the context.
I can register them with my context like this:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getBeanFactory().registerSingleton(..., ...);
But it doesn't feel right, and autowiring doesn't work. How do I register these with my context?
EDIT: for the autowiring, I have noted Sean Patrick Floyd response here: How to get beans created by FactoryBean spring managed? but this still doesn't answer my question.