4 Replies Latest reply: Sep 1, 2010 9:18 AM by nimo stephan RSS

PostConstruct and PreDestroy of Bean

nimo stephan Master

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;}

        public void initialize()
                System.out.println("Instance of B was constructed via inject.");

           public void retrieveRecordsOfB(){


        public void preDestroy()
                System.out.println(this.getClass().getSimpleName() + " was destroyed.");


and referred this bean in jsf-page:

<h:commandButton action="#{a.retrieveRecordsOfB}" value="retrieve"/>

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?