0

I have a maven Java app with different projects for (model, services and UI). I am trying to Inject the service within the UIBean.java file. However it fails to do so and gives an error that the Service is null.

The below is the code for the UIBean.java

package com.test.testhr.ui;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.test.testhr.model.User;
import com.test.testhr.service.authentication.ILogInService;

@ManagedBean(name = "loginUIBean")
@Component
@SessionScoped
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class LoginUIBean {

    private String username;

    private String password;

    private String messages = new String("");

    @Inject
    private transient ILogInService logInService;

    public void login(ActionEvent actionEvent) {
        User user = new User();
        user.setUsrName(username);
        user.setUsrPasswd(password);

        try {
            user = logInService.validate(user);
            if (null != user) {
                HttpSession httpSession = (HttpSession) FacesContext
                        .getCurrentInstance().getExternalContext()
                        .getSession(false);
                httpSession.setAttribute("user", user);
                FacesContext.getCurrentInstance().getExternalContext()
                        .redirect("default.xhtml");
            }
        } catch (Exception ex) {
            messages = "Please provide valid user name and password";
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Failure Message", messages));
        }
    }

However when the program comes to logInService. It finds it as null as the injection has not happened well.

Can someone help?

UPDATE : I dont have a faces-config.xml in my project. Can that be an issue? My web.xml is as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <context-param>  
        <param-name>primefaces.THEME</param-name>  
        <param-value>aristo</param-value>  
    </context-param> 
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>AuthenticationFilter</filter-name>
        <filter-class>com.test.testhr.core.ui.servlet.AuthenticationFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>AuthenticationFilter</filter-name>
        <url-pattern>/faces/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener> 

</web-app>
Kalpesh Thavrani
  • 91
  • 2
  • 2
  • 5
  • Possible duplicate of http://stackoverflow.com/questions/15341877/how-to-inject-a-cdi-bean-in-a-managedbean – Wundwin Born Jul 02 '14 at 13:21
  • Your bean is a spring bean and a jsf bean. Leading to 2 instances of the bean one with the dependencies injected (the spring managed one) and one without them injected (the jsf managed one). Remove `@Component` and use `@ManagedProperty` instead of `@Inject`. Or remove `@ManagedBean` and `@SessionScope` and make it a spring managed session scoped bean instead. – M. Deinum Jul 02 '14 at 13:37
  • @Denium : Tried both but didnt work. – Kalpesh Thavrani Jul 02 '14 at 16:05
  • @suninsky : Checked the link but doesnt work. – Kalpesh Thavrani Jul 02 '14 at 16:06
  • Can you share with use your web.xml, because if you don't use spring mvc and then DispatcherServlet to manage the incoming request, you have to add a Listener in you web xml to manage request,session and global-session scope – Skizzo Jul 02 '14 at 18:33
  • @Skizzo : Have added web.xml file. + I dont have any faces-config.xml file in the project. Hope that is not an issue. – Kalpesh Thavrani Jul 02 '14 at 19:11

0 Answers0