服务注入在托管bean中使用@Inject失败

时间:2014-07-02 13:13:41

标签: java spring dependency-injection

我有一个maven Java应用程序,其中包含(模型,服务和UI)的不同项目。 我试图在UIBean.java文件中注入服务。但是它没有这样做并且给出了服务为空的错误。

以下是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));
        }
    }

然而,当程序来到logInService时。它发现它为空,因为注射没有发生。

有人可以帮忙吗?

更新:我的项目中没有faces-config.xml。这可能是个问题吗? 我的web.xml如下:

<?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>

0 个答案:

没有答案