@Inject annotation for object and two different behaviors of this object

时光怂恿深爱的人放手 提交于 2019-12-12 03:26:39

问题


I use a JSF 2.2 and CDI. I created below example to show you a heart of my problem. You can see below:

  • the two xhtml pages,
  • the session scoped CDI bean (LoginController) which is the controller for the xhtml page1,
  • the view scoped CDI bean (DataController) which is controller for the xhtml page2,
  • the request scoped CDI bean (DataServiceImpl) which is the service for theDataController bean.

This is a use case and flow of the data:

xhtml page2 -> the DataController controller -> call dataService.addData() in send() method (which is located in the DataController class) -> execute addData() method (which is located in the DataServiceImpl class)

The heart of my problem:

  • If I inject the LoginController to the DataServiceImpl class, the loginController object doesn't work (i.e. it returns null) [Option 1 in the code below].
  • If I inject the LoginController to the DataController class, the loginController object works fine (i.e. it returns what I want) [Option 2 in the code below].

My question is: Why this injected object behaves in two different ways depending on place of injection?

This is the part of my xhtml page1:

<h:form>               

    <p:growl id="growl" showDetail="false" />

    <h:inputText id="username" value="#{userLogin.username}" label="Username" required="true" requiredMessage="Username: This field is required." title="Enter your username." pt:placeholder="Username" />
    <h:inputSecret  id="password" value="#{userLogin.password}" label="Password" required="true" requiredMessage="Password: This field is required." title="Enter your password." pt:placeholder="Password" />
    <p:commandButton value="Login" action="#{loginController.login}" update="growl" />                  

</h:form>

This is the part of my xhtml page2:

<h:form>    

    <p:growl id="growl" showDetail="false" />

    <p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign">
        <p:outputLabel for="data" value="Data:" />
        <p:inputText  id="data" value="#{data.text}" required="true" requiredMessage="Data: This field is required." />
        <p:commandButton id="buttonSend" value="Send" action="#{dataController.send()}" update="messages" />                        
    </p:panelGrid>                                              

</h:form>

This is the session scoped CDI bean:

@Named
@SessionScoped
public class LoginController implements Serializable {

    private static final long serialVersionUID = -6322113716363932422L;

    public String login(){      

        if(userService.login(userLogin)){

            currentUser=userService.getCurrnetUser(userLogin.getUsername());
            return "home?faces-redirect=true";          
        }
        else{

            facesContext.addMessage(null, new FacesMessage("Data entered are incorrect"));
            return null;
        }
    }

    public String logout(){

        currentUser=null;
        return "login?faces-redirect=true";
    }

    public boolean isLoggedIn() {

          return currentUser!=null;
    }

    @Produces
    @LoggedIn
    public UserAccount getCurrentUser(){

        return currentUser; 
    }   

    @Inject
    private FacesContext facesContext;

    @Inject
    private UserServiceImpl userService;

    @Named
    @Produces
    @RequestScoped
    private UserAccount userLogin=new UserAccount();

    private UserAccount currentUser;
}

This is the view scoped CDI bean:

@Named
@ViewScoped
public class DataController implements Serializable {

    private static final long serialVersionUID = 1383572529241805730L;

    public void send(){

        /* OPTION 2
         * If I inject the LoginController here 
         * instead of in DataServiceImpl bean, the loginController object 
         * works fine (i.e. it isn't null and returns the name of the user). 
         */
        String name=loginController.getCurrentUser().getName();

        dataService=new DataServiceImpl();
        dataService.addData(data.getText);
    }

    @Named
    @Produces
    @RequestScoped
    private Data data=new Data();  

    @Inject
    private DataService dataService;

    @Inject
    private LoginController loginController;    
}

This is the request scoped CDI bean:

@Named
@RequestScoped
public class DataServiceImpl implements DataService {

    @Override
    public void addData(String data) {

        /* OPTION 1
         * If I inject the LoginController here 
         * instead of DataController bean, the loginController object 
         * doesn't work (i.e. returns null. I get the NullPointerException exception 
         * in line below due to the loginController object which is null)
         */
        String name=loginController.getCurrentUser().getName();

        //Proccess some data
    }

    @Inject
    private LoginController loginController;
}

回答1:


I don't really understand your design, and this may be part of the problem.

You shouldn't inject a controller into another controller, or inject a controller into a service.

Seeing that your LoginController produces a @LoggedIn UserAccount, why not simply inject that, instead of injecting the loginController and calling loginController.getCurrentUser().getName()?



来源:https://stackoverflow.com/questions/36228557/inject-annotation-for-object-and-two-different-behaviors-of-this-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!