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>