@PostConstruct使用自定义视图范围多次调用

时间:2013-11-05 14:14:58

标签: spring jsf view-scope postconstruct

我有一个使用JSF 2.0,mojarra 2.2.4,Tomcat 7和Primefaces 3.5的项目。我们还使用Spring和Hibernate。

我遇到有关@PostContruct anotated方法的问题。它只应该在创建页面时调用一次,而是调用它超过10次。当我按下<p:commandButton/>时也会调用它 SessionScoped Beans不会发生多次调用,只能使用我的自定义ViewScoped Bean。

这是自定义ViewScope实施:

ViewScope

public class ViewScope implements Scope {

@Override
@SuppressWarnings("rawtypes")
public Object get(String name, ObjectFactory objectFactory) {       
    if (FacesContext.getCurrentInstance().getViewRoot() != null) {
        Map<String, Object> viewMap = FacesContext.getCurrentInstance()
            .getViewRoot().getViewMap();

        if (viewMap.containsKey(name)) {                
            Object bean = viewMap.get(name);

            // restore a transient autowired beans after re-serialization bean
            WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
            AutowireCapableBeanFactory autowireFactory = webAppContext.getAutowireCapableBeanFactory();
            if (webAppContext.containsBean(name)) {                 
                // Reconfigure restored bean instance.
                bean = autowireFactory.configureBean(bean, name);
            }
            // end restore

        return bean;
        } else {
            Object object = objectFactory.getObject();
            viewMap.put(name, object);
            return object;
        }
    }else{
        return null;
    }   
}

@Override
public Object remove(String name) {

    return FacesContext.getCurrentInstance().getViewRoot().getViewMap()
            .remove(name);
}

public String getConversationId() {
    return null;
}

@Override
public void registerDestructionCallback(String name, Runnable callback) {
}

public Object resolveContextualObject(String key) {
    return null;
}

 public void clearBean() { FacesContext.getCurrentInstance()
        .getViewRoot().getViewMap().clear();
}

}

The Bean

@SuppressWarnings("serial")
@Component("grupoFormBean")
@Scope("view")
public class GrupoFormBean implements Serializable {
Collection<UIViewParameter> viewParams = ViewMetadata.getViewParameters(FacesContext.getCurrentInstance().getViewRoot());

@PostConstruct
public void init(){
    System.out.println("=========== entering PostConstruct =============");
    this.comboSistema = sistemaService.findAll();
    System.out.println("=========== leaving PostConstruct =============");
}   

public void prepara(){
    if(!FacesContext.getCurrentInstance().isPostback()){
        if(grupoId==null){              
            return;
        }else{              
            this.alterar = true;
            setGrupo(grupoService.findById(grupoId));
            prepareAlterar();               
        }
    }
}
//implementation of others components
//getters and setters

}

XHTML页面

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <ui:composition template="../WEB-INF/templates/page.xhtml"
      xmlns="http://www.w3.org/1999/xhtml"      
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <ui:define name="content">

    <f:metadata>
    <f:viewParam name="idgrupo" id="idgrupo" value="#{grupoFormBean.grupoId}"/>
        <f:event type="preRenderView" listener="#{grupoFormBean.prepara}">
        </f:event>
    </f:metadata>

        <div class="titulo-pagina">

            <h2>Cadastro de Grupo</h2>

        </div>

        <h:form id="formulario" styleClass="formularios">

    //... page stuff

    </h:form>

    </ui:define>
</ui:composition>

这是我第一次进入页面时的日志:

=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as    SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as     SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============
=========== entering PostConstruct =============
Hibernate: select sistema0_.ID as ID3_, sistema0_.NOME as NOME3_, sistema0_.SIGLA as SIGLA3_, sistema0_.VERSAOATUAL as VERSAOAT4_3_ from SCA.SISTEMA sistema0_ order by sistema0_.NOME
=========== leaving PostConstruct =============

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!-- View scope -->
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="view">
                <bean class="mypackage.util.ViewScope"/>
            </entry>
        </map>
    </property>
</bean>

</beans>

0 个答案:

没有答案