@Autowired bean null 到 JSF ManagedBean 中,使用 Spring xml 配置



我使用 xml 配置来声明我的 UserDao bean,并将其调用到另一个组件中:AuthenticationFacade(由@Component注释声明(,使用如下所示@Autowired注释:

应用程序上下文.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.medkhelifi.tutorials.todolist"/>
<import resource="classpath:/conf/applicationContext-db.xml"/>
<import resource="classpath:/conf/applicationContext-security.xml"/>
</beans>

applicationContext-db.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--.....-->
<!--        DAO BEANS                       -->
<bean id="userDao" class="com.medkhelifi.tutorials.todolist.models.dao.impl.UserDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--....->
</beans>

身份验证外观.java

package com.medkhelifi.tutorials.todolist.components;
import com.medkhelifi.tutorials.todolist.models.dao.UserDao;
import com.medkhelifi.tutorials.todolist.models.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import javax.faces.bean.ManagedBean;
@Component
@ManagedBean
@Scope("session")
public class AuthenticationFacade implements IAuthenticationFacade {
@Autowired
private UserDao userDao;
public Authentication getAuthentication() {
return SecurityContextHolder.getContext().getAuthentication();
}
public User getAuthenticatedFacade() {
Authentication authentication = getAuthentication();
User user = userDao.findByUsername(authentication.getName());
return user;
}
}

使用此配置,我将userDao设置为null,我不知道我是否错过了什么。

我在那里使用我的身份验证外观托管Bean:

索引.xhtml

<h:body>
<ui:composition template="templates/layout.xhtml">
<ui:define name="content">
<b:row>
<b:navBar brand="Brand" brandHref="#" fluid="true">
  <!-- Following line is needed for TBS 3.0.1 (panel content overflow issue) -->
<b:navbarLinks pull="right"><b:navLink value="    " href="#"></b:navLink></b:navbarLinks>
    <b:navbarLinks pull="right" styleClass="hidden-xs">
            <b:dropMenu value="#{authenticationFacade.getAuthenticatedFacade().firstname}">
            <b:navLink value="logout" href="#"></b:navLink>
        </b:dropMenu>
    </b:navbarLinks>
</b:navBar>
</b:row>
</ui:define>
</ui:composition>
</h:body>

经过几个小时的搜索,我找到了我搞砸的地方: 我忘了在我的脸配置.xml中添加SpringBeanFacesELResolver

人脸配置.xml

<application>
<!-- I forgot to add this resolver to my faces-config.xml -->
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application>

最新更新