0

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)
jetty
  • 859
  • 2
  • 17
  • 37
  • Is `Parent` defined as a Spring bean? Dependency injection only works in Spring beans, not in other classes which are not Spring beans. Also, it does not work when you create an instance of the class using `new`; you need to let Spring manage the beans. – Jesper May 25 '17 at 07:27
  • Yes it is . I wil correct the sample code. – jetty May 25 '17 at 07:29
  • `class DesignContextService` should also be annotated with `@Service` or `@Component` in order for spring to autowire it correctly. Maybe that's your issue? – mingos May 25 '17 at 07:34
  • 1
    Post **real** code, indent it properly, post the stack trace of the exception, and try providing a complete minimal example. I don't even know how the someFunction method could be called, since it's private. Your posted code wouldn't even compile. – JB Nizet May 25 '17 at 07:35
  • DesignContextService is annotated with @Component – jetty May 25 '17 at 07:35
  • Do you get a `NullPointerException`in your `init()` method or some other error from spring? – P.J.Meisch May 25 '17 at 07:47

1 Answers1

0

This is because of the @lazy annotation. As specified in the documentation:

The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup. Pre-instantiation means that an ApplicationContext will eagerly create and configure all of its singleton beans as part of its initialization process. Generally this is a good thing, because it means that any errors in the configuration or in the surrounding environment will be discovered immediately (as opposed to possibly hours or even days down the line).

Please look into the following link for reference: Using Spring @Lazy and @PostConstruct annotations

Spring: how to initialize related lazy beans after main bean creation

KayV
  • 12,987
  • 11
  • 98
  • 148