0

I have issues when I test these 3 cases, my problem is about annotations

1st case

Description : use @Component and @ViewScoped, the function ajouterCommentaire() work properly, this means that @Autowired work properly

Issue : the @PosContruct init mehtod give a NullPointerException when executing the function obtenirUserClient()

package com.andriasoft.mppm.client.controller;

import com.andriasoft.mppm.entity.Carte;
import com.andriasoft.mppm.entity.Commentaire;
import com.andriasoft.mppm.entity.Defunt;
import com.andriasoft.mppm.entity.UtilisateurClient;
import com.andriasoft.mppm.repository.CommentaireRepository;
import com.andriasoft.mppm.repository.UtilisateurClientRepository;
import com.andriasoft.mppm.utils.Utils;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author nathan
 */
@Component("MBLivreDor")
@ViewScoped
public class MBLivreDor implements Serializable {

    @Autowired
    UtilisateurClientRepository utilisateurClientRepository;
    @Autowired
    CommentaireRepository commentaireRepository;

    private UtilisateurClient userClient;
    private Carte carte;
    private Defunt defunt;
    private List<Commentaire> mainComments;
    private Commentaire commentaire = new Commentaire();

    public MBLivreDor() {
    }

    @PostConstruct
    private void init() {
        try {
            userClient = obtenirUserClient();
            carte = userClient.getCarte();
            defunt = carte.getDefunt();
            mainComments = commentaireRepository.findMainCommentByCarte(carte);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void ajouterCommentaire() {
        commentaire.setCarte(carte);
        commentaire.setCreatedAt(new Date());
        commentaire.setNote(0);
        commentaire.setUtilisateurClient(userClient);
        commentaireRepository.save(commentaire);
        commentaire = new Commentaire();
        mainComments = commentaireRepository.findMainCommentByCarte(carte);
    }

    public UtilisateurClient obtenirUserClient() {
        UtilisateurClient utilisateurClient = (UtilisateurClient) Utils.getRequest().getSession().getAttribute("utilisateurClient");
        return utilisateurClient;
    }

}

2nd case

Description : use @ManagedBean and @ViewScoped, @PostConstruct init() work properly

Issue : the function ajouterCommentaire() doesn't work properly, the problem is about the @Autowired annotation, it's not detected

package com.andriasoft.mppm.client.controller;

import com.andriasoft.mppm.entity.Carte;
import com.andriasoft.mppm.entity.Commentaire;
import com.andriasoft.mppm.entity.Defunt;
import com.andriasoft.mppm.entity.UtilisateurClient;
import com.andriasoft.mppm.repository.CommentaireRepository;
import com.andriasoft.mppm.repository.UtilisateurClientRepository;
import com.andriasoft.mppm.utils.Utils;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author nathan
 */

@ViewScoped
@ManagedBean(name = "MBLivreDor")
public class MBLivreDor implements Serializable {

    @Autowired
    UtilisateurClientRepository utilisateurClientRepository;
    @Autowired
    CommentaireRepository commentaireRepository;

    private UtilisateurClient userClient;
    private Carte carte;
    private Defunt defunt;
    private List<Commentaire> mainComments;
    private Commentaire commentaire = new Commentaire();

    public MBLivreDor() {
    }

    @PostConstruct
    private void init() {
        try {
            userClient = obtenirUserClient();
            carte = userClient.getCarte();
            defunt = carte.getDefunt();
            mainComments = commentaireRepository.findMainCommentByCarte(carte);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void ajouterCommentaire() {
        commentaire.setCarte(carte);
        commentaire.setCreatedAt(new Date());
        commentaire.setNote(0);
        commentaire.setUtilisateurClient(userClient);
        commentaireRepository.save(commentaire);
        commentaire = new Commentaire();
        mainComments = commentaireRepository.findMainCommentByCarte(carte);
    }

    public UtilisateurClient obtenirUserClient() {
        UtilisateurClient utilisateurClient = (UtilisateurClient) Utils.getRequest().getSession().getAttribute("utilisateurClient");
        return utilisateurClient;
    }

}

3rd case

Description : use @Component, @Scope, @ManagedBean

Issue : the issue is the same as the 1st case

package com.andriasoft.mppm.client.controller;

import com.andriasoft.mppm.entity.Carte;
import com.andriasoft.mppm.entity.Commentaire;
import com.andriasoft.mppm.entity.Defunt;
import com.andriasoft.mppm.entity.UtilisateurClient;
import com.andriasoft.mppm.repository.CommentaireRepository;
import com.andriasoft.mppm.repository.UtilisateurClientRepository;
import com.andriasoft.mppm.utils.Utils;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author nathan
 */
@Component("MBLivreDor")
@Scope("singleton")
@ManagedBean(name = "MBLivreDor")
public class MBLivreDor implements Serializable {

    @Autowired
    UtilisateurClientRepository utilisateurClientRepository;
    @Autowired
    CommentaireRepository commentaireRepository;

    private UtilisateurClient userClient;
    private Carte carte;
    private Defunt defunt;
    private List<Commentaire> mainComments;
    private Commentaire commentaire = new Commentaire();

    public MBLivreDor() {
    }

    @PostConstruct
    private void init() {
        try {
            userClient = obtenirUserClient();
            carte = userClient.getCarte();
            defunt = carte.getDefunt();
            mainComments = commentaireRepository.findMainCommentByCarte(carte);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void ajouterCommentaire() {
        commentaire.setCarte(carte);
        commentaire.setCreatedAt(new Date());
        commentaire.setNote(0);
        commentaire.setUtilisateurClient(userClient);
        commentaireRepository.save(commentaire);
        commentaire = new Commentaire();
        mainComments = commentaireRepository.findMainCommentByCarte(carte);
    }

    public UtilisateurClient obtenirUserClient() {
        UtilisateurClient utilisateurClient = (UtilisateurClient) Utils.getRequest().getSession().getAttribute("utilisateurClient");
        return utilisateurClient;
    }

}

So, how to anotate my JSF managed bean to make both of these functionnality to work :

  • @PostConstruct init method when rendering the page caller, or the equivalent in Spring
  • @Autowired, to be detected
Aroniaina
  • 1,252
  • 13
  • 31
  • Use plain spring annotations and configure an additional EL resolver. That makes all Spring annotated beans available to jsf – Kukeltje Jan 18 '16 at 08:45
  • And the viewsoped annotation on the first example only works on jsf @ManagedBean annotated beans. Not Spring ones afaik – Kukeltje Jan 18 '16 at 08:47
  • @kukeltje Already use spring EL resolver in faces-config.xml. So what is the right value to replace the PostConstruct in plain Spring? – Aroniaina Jan 18 '16 at 08:54
  • No idea, not a spring user. Never was,never will bw – Kukeltje Jan 18 '16 at 09:05
  • Apart from the `NullPointerException` in the first case (which should not have caused), the scoped annotation `javax.faces.bean.ViewScoped` is not detectable as that bean is supposed to be managed by Spring. Thus, it defaults to the Spring "singleton" scope. In the second case, `@Autowired` remains silent because that bean is attempted to be designated with JSF related annotations while beans are supposed to be managed by Spring. In the last example, you are trying to use both Spring and JSF together using the corresponding annotations from both of them which is disallowed. Use either of them. – Tiny Jan 18 '16 at 09:23
  • In essence, you will need `@Component("bean")` and `@Scope("ViewScoped")` along with configuring `org.springframework.web.jsf.el.SpringBeanFacesELResolver` in `faces-config`. View scope is supported by Spring you will need to create a custom view scope on your own. You could also use another bean management framework, CDI, if you want. – Tiny Jan 18 '16 at 09:23
  • @BalusC, thanks for the link of the duplicate! But I already saw this topic! My question is, as writed at the end : how to make both of the two function to work, init the managedbean on view rendered and detect @Autowired! – Aroniaina Jan 18 '16 at 10:15
  • Just follow the correct approach in the dupe. All your attempts so far are wrong as clearly explained in dupe. – BalusC Jan 18 '16 at 10:17
  • Found solution using ManagedBean and ViewScoped (2nd case)! http://codereview.stackexchange.com/questions/23790/spring-autowiring-in-managed-beans-with-support-for-serialization-is-this-safe – Aroniaina Jan 20 '16 at 12:26

0 Answers0