dependency injection - Spring @PostContruct not waiting for complete bean creation -
while setting service have initialization method using @postconstruct, i'm calling service has been autowired. method calling service in turn uses service method has been autowired it, when service called, npe. indeed, autowired fields in second service null. having original autowired service extend applicationcontextaware proved service had not been initialized (or @ least had not been made context aware).
it seems @postconstruct being called after service has been made context aware, it's not waiting until rest of application context aware. how can either have wait on dependencies or change initialization it's called @ later point in spring's initialization path?
example:
@service public class servicea { @autowired private serviceb serviceb; @postconstruct public void init() { serviceb.somemethod(); } } @service public class serviceb implements applicationcontextaware { @autowired private servicec servicec; // null @ time of @postconstruct private applicationcontext applicationcontext; public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { this.applicationcontext = applicationcontext; // never gets called } public void somemethod() { serviceb.someothermethod(); // nullpointerexception } }
Comments
Post a Comment