Skip to main content
3 of 3
added 30 characters in body
Sean Patrick Floyd
  • 300.6k
  • 72
  • 481
  • 598

Because Spring can't access any fields or methods before the object is created (which is done through the constructor). So Spring instantiates the object using the constructor and then injects the properties.

The only way around this is to use Constructor Injection (which can be cumbersome if you have multiple dependencies). I think what you should do is move your code out of the constructor and into an initialization method using the @PostConstruct annotation:

@PostConstruct public void init(){ // do stuff with entitymanager here } 
Sean Patrick Floyd
  • 300.6k
  • 72
  • 481
  • 598