@PostConstruct 方法为同一个请求调用两次 [英] @PostConstruct method called twice for the same request

查看:25
本文介绍了@PostConstruct 方法为同一个请求调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JSF 2.0 和 GlassFish 3.0.

I'm using JSF 2.0 with GlassFish 3.0.

我有以下托管 Bean:

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

overview.xhtml 文件中,我从我的 OverviewController 中调用了不同的属性或方法.

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

如您所见,init() 方法在同一个请求中被无缘无故地调用了两次.据我所知,任何用 PostConstruct 注释的方法都会在每个请求中调用一次.我错了吗?

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?

页面上没有使用 AJAX.我用萤火虫检查了请求的数量.有树请求:

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

  • 1.一个用于 javax.faces.resource (GET)
  • 2.一个用于css文件(GET)
  • 3.一个用于overview.xhtml (GET)

推荐答案

如果您有多个框架管理同一个 bean 类,就会发生这种情况.例如.JSF and CDI,或 JSF and Spring,或 CDI and Spring 等.仔细检查 bean 上的配置和注释.

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.

如果您使用 CDI 并且在整个课程中使用多个 @Named 注释,也会发生这种情况.例如,一个 @Named 直接在类上将其注册为托管 bean,另一个在 @Produces getter 方法上.你需要问问自己这是否真的必要.您也可以只使用 #{bean.someObject} 而不是 #{someObject}.

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() {
        // ...
    }

}

如果您的托管 bean 扩展了一些抽象类,而该抽象类又在该方法上具有 @PostConstruct,也会发生这种情况.您应该从中删除注释.或者,您应该使 init 方法抽象并且不要在实现 bean 上具有 @PostConstruct:

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();

}

这篇关于@PostConstruct 方法为同一个请求调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆