0

I have a project using JSF 2.0 , mojarra 2.2.4, Tomcat 7 and Primefaces 3.5. We also use Spring and Hibernate.

I am facing a problem regarding the @PostContruct anotated method. It should be called only once, when the Page is created, but instead it is called more than 10 times. It is also called when I press a <p:commandButton/> The multiple calls doesn't occur with SessionScoped Beans, only with my custom ViewScoped ones.

This is the Custom ViewScope implementation:

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 Page

    <?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>

This is the log of when I first enter the page:

=========== 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>
Anika
  • 398
  • 5
  • 23
  • The only thing we are sure is that your custom `ViewScope` implementation is broken and turns to be `@NoneScoped` (which is worse than `@RequestScoped`). Where did you get the custom implementation? – Luiggi Mendoza Nov 05 '13 at 14:30
  • It came with the base project I had to use as reference. – Anika Nov 05 '13 at 14:36
  • Are you sure you have configured your new custom scope accordingly? – Luiggi Mendoza Nov 05 '13 at 14:39
  • Yes. I did just like it here http://docs.spring.io/spring/docs/3.0.0.RC2/spring-framework-reference/html/ch03s05.html I'll post the configuration just in case... – Anika Nov 05 '13 at 14:49
  • I have tried implementing the `porting viewscope` from `JSF` to `Spring` but didn't work properly. Not Sure it's exactly the way to implement but as it came from `primefaces` founder it should be working properly. Check my answer in this post whether it's useful to you http://stackoverflow.com/questions/13005421/jsf-view-scope-in-spring-3-0. I used `native JSF viewscope` with `serialization methods` to get back spring bean which goes null at the time of `Serialization` – SRy Nov 05 '13 at 15:07
  • 1
    Instead of `configureBean` use `autowireBean`. The first also calls all callback nmethods (like `@PostConstruct` annotated methods) whereas the latter only applies autowiring. – M. Deinum Nov 05 '13 at 15:23

0 Answers0