I have the following scenario where I am running into null pointer exception because bean not getting initialized and leading to failure in my server failing to boot. There is a newly introduced call in PostConstruct annotated method which is failing. The same call is being made in another method which is not in PostConstruct which executes correctly and not cause any issue.
@Component
@Lazy
@Primary
class Parent{
@Autowired
private DesignContextService designContextService;
@PostConstruct
private void init(){
designContextService.getMethod();// fails
}
private void someFunction(){
designContextService.getMethod();// executes successfully
}
}
}
Class DesignContextService{
@Autowired
private ContextService contextService;
public void getMethod(){
contextService.isContextCreated();
...
}
// Below classes present in another jar
class ContextService{
@Inject
public ContextAdapter contextAdapter;
public void isContextCreated(){
contextAdapter.isEstablished();// contextAdapter is null . Throws exception here
}
}
}
Error Stack trace :
at
Caused by org.springframework.beans.factory.BeanCreationException : Error creating bean ...
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)