使用Spring xml配置,将@Autowired bean null转换为JSF ManagedBean

时间:2018-10-12 19:38:06

标签: java spring autowired xml-configuration

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

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

AuthenticationFacade.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,我不知道我是否错过了一些事情。

在哪里可以使用我的AuthenticationFacademanagedBean:

index.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>

1 个答案:

答案 0 :(得分:0)

经过几个小时的搜索,我发现自己混乱了: 我忘记将SpringBeanFacesELResolver添加到我的faces-config.xml中。

faces-config.xml

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