@PostConstruct method called twice for the same request

 ̄綄美尐妖づ 提交于 2019-12-17 15:46:11

问题


I'm using JSF 2.0 with GlassFish 3.0.

I have the following Managed Bean:

@ManagedBean
@RequestScoped
public class OverviewController{

    private List<Event> eventList;

    @PostConstruct
    public void init(){
        System.out.println("=> OverviewController - init() - enter");

        System.out.println("=< OverviewController - init() - exit");
    }
}

From the the overview.xhtml file I'm calling different attributes or methods from my OverviewController.

<ui:repeat var="event" value="#{overviewController.eventList}">
    ...
</ui:repeat>

Everything works just fine but the problem is on the Log File:

INFO: Enter : RESTORE_VIEW 1
INFO: Exit : RESTORE_VIEW 1

INFO: Enter : RENDER_RESPONSE 6
INFO: => OverviewController - init() - enter
INFO: => Overview Controller - updateSelectedTab() - enter
INFO: =< Overview Controller - updateSelectedTab() - exit
INFO: =< OverviewController - init() - exit
INFO: => OverviewController - init() - enter
INFO: => Overview Controller - updateSelectedTab() - enter
INFO: =< Overview Controller - updateSelectedTab() - exit
INFO: =< OverviewController - init() - exit
INFO: Exit : RENDER_RESPONSE 6

As you can see, The init() method is called twice in the same request for no reason what so ever. From what I know, any method annotated with PostConstruct is called once every request. Am I wrong?

EDIT: No AJAX is used on the page. I checked the number of requests with firebug. There are tree requests made:

  • 1.One for the javax.faces.resource (GET)
  • 2.One for the css file (GET)
  • 3.One for overview.xhtml (GET)

回答1:


That can happen if you have multiple frameworks managing the same bean class. E.g. JSF and CDI, or JSF and Spring, or CDI and Spring, etc. Doublecheck your configuration and annotations on the bean.

That can also happen if you're using CDI and are using multiple @Named annotations throughout the class. For example, a @Named straight on the class to register it as a managed bean and another one on a @Produces getter method. You'd need to ask yourself whether that is really necessary. You could also just use #{bean.someObject} instead of #{someObject}.

@Named
@RequestScoped
public class Bean {

    @PostConstruct
    public void init() {
        // ...
    }

    @Named
    @Produces
    public SomeObject getSomeObject() {
        // ...
    }

}

That can also happen if your managed bean extends some abstract class which has in turn also a @PostConstruct on the method. You should remove the annotation from it. Alternatively, you should make the init method abstract and not have @PostConstruct on the implementing bean:

public abstract class BaseBean {

    @PostConstruct
    public void postConstruct() {
        init();
    }

    public abstract void init();

}



回答2:


It is possible that the both init() method and @PostConstruct methods are firing and causing this behavior. Try changing the name of the init() method and/or putting it private. I think that this may be related to your problems:

http://javahowto.blogspot.com/2011/07/servlet-init-method-vs-postconstruct.html

I also found a good post about debugging JSF life cycles here: Debug JSF lifecycle



来源:https://stackoverflow.com/questions/9077966/postconstruct-method-called-twice-for-the-same-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!