I am wondering if there is a way to initialize with already created dependency objects?
Suppose I have the following class:
class MyPage { @Autowired LogStorage storage; private String applicationId; private Date date; public MyPage(String applicationId, Date date) { this.applicationId = applicationId; this.date = date; } public String render() { Collection<Entry> entries = storage.getEntries(applicationId, date);
I want to break object creation into two phases. First of all, we create an object with user-provided data ( applicationId and date in this example), and the second initializes the object using Spring dependencies of the DI container. Therefore, I need some initializing object already created. I think the client code should look something like this:
MyPage p = new MyPage(applicationId, date); beanFactory.initDependencies(p); String html = p.render();
The client does not know (and should not in my case) all the dependencies needed by the page object, so I cannot explicitly provide the dependencies. But I refer to the BeanFactory object and can delegate this task to Spring. Are there any ways to do this?
source share