I have a Spring Bean which has a @Autowired setter getter injection. However, when i try to access injected bean, i get NullPointerException because the injected bean has not be really injected.
Is there any way to guarantee that the injection is done before constructure call?
@Component
@Scope("session")
public class A{
@Autowired
B;
public A()
{
//B is null here, because it has not been injected yet.
}
//Setter Getters
}
Use the
@PostConstructannotation. This will be called right after Spring initializes your bean.It’s from the javax.* API