0

I have decided to migrate an application in the architecture JPA-EJB-JSF toward JPA-Spring-JSF.

During this migration to spring I have enabled CDI also. After the migration of all the layers I am at the level of the layer of the controllers. In Java EE I have annotated methods with @PostConstruct which allow me to initialize variables,test configurations for the applied.These methods are running before before the setters/getters.

Example Java EE code:

@ManagedBean(name="classecontroller")
@SessionScoped
public class ClasseController implements Serializable {

    private Etablissements etablissement;
    private Classe classe = new Classe();
    private Niveau niveau = new Niveau();
    private Personne currentUser;

    public ClasseController() {}

    @PostConstruct
    void initialiseSession() {
        etablissement = new Etablissements();

        Etablissements testEtab = null;
        testEtab = (Etablissements) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(GlobalFonctions.ETAB_ACTIF);
        if (testEtab != null) {
            etablissement = testEtab;
        } else {
            etablissement = (Etablissements) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(GlobalFonctions.ETAB_ACTIF);
        }
    }
    // setter getters
}

After migration on Spring I have read that the @PostConstruct is only executed after the initialization of the beans in the context.Therefore I have found that my methods that should be executed at the start of my application no longer run during the opening of a web page using my controller.

I would like to know how I can implement this without too many changes to the code?

here is the code Spring:

@Named("classecontroller")
@SessionScoped
public class ClasseController implements Serializable {

    private Etablissements etablissement;
    private Classe classe = new Classe();
    private Niveau niveau = new Niveau();
    private Personne currentUser;

    public ClasseController() {}

    @PostConstruct
    void initialiseSession() {
        etablissement = new Etablissements();

        Etablissements testEtab = null;
        testEtab = (Etablissements) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(GlobalFonctions.ETAB_ACTIF);
        if (testEtab != null) {
            etablissement = testEtab;
        } else {
            etablissement = (Etablissements) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(GlobalFonctions.ETAB_ACTIF);
        }
    }
// setter getters
}

Thanks

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • My first question: Why the migration? – Buhake Sindi Jan 15 '15 at 15:36
  • 1
    "*here is the code Spring*". One bean is managed by the JSF framework and the other is managed by the container - the Weld implementation - CDI. In those present two code snippets, there is nothing like Spring involved - no bean is managed by the Spring framework in those code snippets. – Tiny Jan 15 '15 at 15:52
  • @Tiny if you have CDI enabled, you don't have to worry about Spring specific CDI injections as Spring framework is (should be) Java EE compliant (particularly with CDI). – Buhake Sindi Jan 15 '15 at 16:08
  • You mean your `@PostConstruct` is no longer executing? – kolossus Jan 15 '15 at 16:48
  • @BuhakeSindi i am migrating because of server.we want to host application and cost of the lightheight server is better. – brunel Fabrice Touopi Touopi Jan 16 '15 at 11:54
  • .My @PostConstruct in JavaEE normaly is executed when the controller is called.But now with CDI and Spring,Spring execute my postConstruct method when my application start. – brunel Fabrice Touopi Touopi Jan 16 '15 at 11:58

0 Answers0