2

我无法解决使用 @Autowired 服务获取 null 的问题。这是我的代码。

我的配置文件

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <context:component-scan base-package="com.vulcan.controller.login" />
    <context:component-scan base-package="com.vulcan.service.login" />
    <context:component-scan base-package="com.vulcan.dao.tenant" /> ........

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/applicationContext.xml</param-value>
     </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>....

面孔-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <variable-resolver>
            org.springframework.web.jsf.DelegatingVariableResolver
        </variable-resolver>
    </application>
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>........

我的代码

LoginServiceImpl.java

@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    TenantDao tenantDao;

    @Transactional
    @Override
    public String getTenantIdentifier(String email, String password) { ....

登录控制器.java

@Component
@Scope("session")
@Qualifier("login")
public class LoginController  {

    private String email;
    private String password;
    private String tenantId;
    
    @Autowired 
    LoginService loginService;

    public void doLogin() {
        this.tenantId = loginService.getTenantIdentifier(email, password); ..

登录.xhtml

<ui:define name="content">
    <h:form id="form">   
        <p:growl id="growl" showDetail="true" life="3000" />  
        <h:panelGrid columns="2" cellpadding="5">  
            <h:outputLabel for="username" value="Username:" />  
            <p:inputText value="#{login.email}" id="username" required="true" label="username"/>
            <h:outputLabel for="password" value="Password:" />  
            <p:inputText value="#{login.password}" id="password" required="true" label="password" type="password" />  
            <f:facet name="footer">  
                <p:commandButton id="loginButton" value="Login" update="growl" action="#{login.doLogin()}"/>  
            </f:facet>  
        </h:panelGrid>  
    </h:form>           
</ui:define>

所以我选择使用 Spring 来管理我所有的 bean。这是我为解决问题所做的工作(但仍然没有结果)

  1. 我尝试使用将 JSF 注入@ManagedBeanLoginController 和@ManagedPropertyLoginService。调用后我得到了空指针doLogin()。它说 LoginService 有空指针。

  2. 之后,我尝试使用 Spring 注入 LoginController,如您在上面看到的,而不是出现此错误。

    /login.xhtml @21,109 value="#{login.email}":目标无法访问,标识符“登录”解析为空

任何人都可以帮助我吗?我已经在这个论坛上关注并尝试了很多建议,但没有任何效果。

4

1 回答 1

1

最后我想通了,我只需要删除@Qualifier 注释并将@Component 注释编辑为@Component("login")。

多么愚蠢的错误……

于 2013-08-10T11:48:56.543 回答