text: PostConstruct and PreDestroy of Beannimo stephan Aug 27, 2010 8:23 AM
  I have something like this:
  @SessionScoped @Named public class A{ @PersistenceContext EntityManager entityManager; @Inject B b; public B getB() {return b;} public void setB(B ob) {this.b = b;} @PostConstruct public void initialize() { System.out.println("@PostConstruct"); System.out.println("Instance of B was constructed via inject."); } public void retrieveRecordsOfB(){ b.setRecords(entityManager.createNamedQuery("getRec").getResultList()); } @PreDestroy public void preDestroy() { System.out.println("@PreDestroy"); System.out.println(this.getClass().getSimpleName() + " was destroyed."); } }
  and referred this bean in jsf-page:
  <h:commandButton
  I explored, that when viewing this page then the PreDestroy-Method is called even the session is not finished - why is the Instance of A instantly destroyed after it was constructed?
  14:17:23,471 INFO [STDOUT] @PostConstruct 14:17:23,471 INFO [STDOUT] Instance of B was constructed via inject. 14:17:23,471 INFO [STDOUT] @PreDestroy 14:17:23,471 INFO [STDOUT] @PreDestroy A was destroyed
  Another point is, that when clicking on
  retrieveRecordsOfB, then the database query is called twice (I guess, because of getter/setter). I have thought that this was solved in JSF2 or Weld?
  1. Re: PostConstruct and PreDestroy of BeanJan Groth Aug 27, 2010 8:47 AM (in response to nimo stephan)
  I'm observing the same behavior with a view-scoped bean (from seam-faces).
  I vaguely remember that I've heard something about a related JBoss-AS-Bug in M4 (which is my setup), but basically I'd like to be sure about the reason.
  so: bug or intention?
  cheers,
  jan
  2. Re: PostConstruct and PreDestroy of BeanJan Groth Aug 27, 2010 8:51 AM (in response to nimo stephan)
  oops: just to make sure - I'm referring to the @PreDestroy part of Nimo's posting.
  Concerning the getter / setter part: ASFAIK, those are called multiple times because of the inherent JSF-lifecycle. This means that there is nothing
  to be resolved, it's just in the inherent nature of the technology....
  3. Re: PostConstruct and PreDestroy of BeanPete Muir Sep 1, 2010 8:57 AM (in response to nimo stephan)
  Sounds very weird, if you can reproduce in a simple cdi app (no seam etc), please file an issue :-)
  4. Re: PostConstruct and PreDestroy of Beannimo stephan Sep 1, 2010 9:18 AM (in response to nimo stephan)
  Hello,
  the reason is found in:
  I had this
  import javax.faces.bean.SessionScoped;
  instead of this
  import javax.enterprise.context.SessionScoped;
  So I imported the jsf-scopes instead of the cdi-scopes.
  Would be nice, if CDI would warn or returns an deployement-error,
  if it detects a scope of a jsf-version instead of a scope of the cdi-version.
  One should not be allowed to use the scope of a jsf-version in a CDI-Environment
  - so this should return a deployement error (or at least a warning)!