我无法解决使用@Autowired服务获取null的问题。这是我的密码。
我的配置文件
applicationContext.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) { ....
LoginController.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); ..
login.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。以下是我为解决问题所做的(但仍然没有结果)
我尝试将JSF
@ManagedBean
注入LoginController,并将@ManagedProperty
注入LoginService。调用doLogin()
后得到空指针。它说登录服务有空指针。在那之后,我尝试使用Spring注入到LoginController,正如你在上面看到的那样,我得到了这个错误。
/login.xhtml@21109值="#{login.email}":目标无法访问,标识符"login"解析为空
有人能帮我吗?我已经在这个论坛上遵循并尝试了许多建议,但都无济于事。
最后我想明白了,我只需要删除@Qualifier annotation并将@Component annotation编辑为@Component("login")。
多么愚蠢的错误。。。