Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在托管bean中使用@Inject时,服务注入失败_Java_Spring_Dependency Injection - Fatal编程技术网

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

Java 在托管bean中使用@Inject时,服务注入失败,java,spring,dependency-injection,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.SessionSc

我有一个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>

主题
亚里士多德
小脸蛋
javax.faces.webapp.FacesServlet
1.
小脸蛋
/面孔/*
上下文配置位置
/WEB-INF/applicationContext.xml
身份验证过滤器
com.test.testhr.core.ui.servlet.AuthenticationFilter
身份验证过滤器
/面孔/*
org.springframework.web.context.ContextLoaderListener

bean的可能副本是spring bean和jsf bean。导致两个bean实例,一个注入了依赖项(spring管理的一个),另一个没有注入依赖项(jsf管理的一个)。删除
@组件
并使用
@ManagedProperty
而不是
@Inject
。或者删除
@ManagedBean
@SessionScope
并将其改为spring托管会话范围的bean。@Denium:尝试了这两种方法,但都不起作用。@suninsky:检查了链接,但都不起作用。能否使用web.xml与共享,因为如果不使用spring mvc,然后使用DispatcherServlet来管理传入的请求,您必须在web xml中添加一个侦听器来管理请求、会话和全局会话范围