@PostConstruct方法针对同一请求两次 [英] @PostConstruct method called twice for the same request

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

问题描述

我正在使用带有GlassFish 3.0的JSF 2.0.

I'm using JSF 2.0 with GlassFish 3.0.

我有以下托管豆:

@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.一个概述.xhtml(GET)
  • 1.One for the javax.faces.resource (GET)
  • 2.One for the css file (GET)
  • 3.One for overview.xhtml (GET)

推荐答案

如果您有多个框架管理同一个bean类,则可能会发生这种情况.例如. JSF CDI,或JSF Spring,或CDI 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上 not 不具有@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天全站免登陆