This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PostConstruct and Dependency Injection

 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From one of the question in WhizLabs diagnostic exam:

WhizLabs Diagnostic Exam wrote:All the dependency injections happen just after the completion of @PostConstruct method.


Is this really correct?
Or is this an errata on WhizLabs?
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an incorrect statement, dependency injection happens before @PostConstruct, just after the constructor runs, so you can use the injected resources in @PostConstruct.
Was this a question or an explanation? If a question, it is a false statement, then ;-)
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raf,

I could not stop my self from appreciating you that you are so helpful to this forum.You answered almost all the questions asked by Tremenin.You are really great.

Regards
Sudhakar Karnati
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is asking to choose the correct statement, and this is a correct choice according to WhizLabs.

Karnati Sudhakar wrote:Raf,

I could not stop my self from appreciating you that you are so helpful to this forum.You answered almost all the questions asked by Tremenin.You are really great.

I highly agree with this.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dependency injection happens before @PostConstruct, just after the constructor runs, so you can use the injected resources in @PostConstruct.

The bold part of the statement (bold from me) isn't quite correct. For example in a stateless session bean you can inject an EntityManager, but it isn't allowed to call methods on this EntityManager.

For more details on what operations are allowed in which methods see core spec 4.4.1, 4.5.2 and 5.5.1.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Tip Ralph.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph is right again, I wasn't thinking of this when I wrote this. However, what I really wanted to say, is that everything is injected.

Thanks guys, but I got a lot of help from coderanch myself, so I am just paying my debt right now ;-) And don't forget about Ralph, who has been correcting all my mistakes!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic