会话范围的CDI托管bean在Spring集成后的行为类似于应用程序范围的bean

时间:2016-02-20 07:04:30

标签: spring spring-security cdi managed-bean

我们希望通过集成Spring CDI + JSF + Spring Security来创建Web应用程序。我们已经配置了所有上述框架,我们认为一切正常,但在实现注销功能时,我们已经意识到JSF的ManagedBeans不应该在它们应该创建时创建。例如:我们在会话范围内创建了托管bean,我们预计在注销和会话无效后将删除bean。

不幸的是,在重新登录之后,托管bean实例仍然存在(每个用户信息都与注销前相同:()。我们添加了更多日志,我们注意到我们的托管bean是在启动应用程序期间创建的,而不是在我们发现它是由context:component-scan base-package =" ..."在spring应用程序上下文配置文件中引起的,所以我们添加了context:exclude-filter type = ""表达式=""但之后不再创建任何托管bean :(。我们在下面添加了一些列表。

的applicationContext.xml

<context:property-placeholder location="classpath:application.properties"/>

<context:component-scan base-package="com.teaman" />
<mongo:repositories base-package="com.teaman.dao" />

<mongo:mongo host="${mongodb.hostname}" port="${mongodb.port}" id="mongo" />
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="${mongodb.dbname}" />
<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mongoConverter" ref="converter"/>
    <property name="writeConcern">
        <util:constant static-field="com.mongodb.WriteConcern.SAFE" ></util:constant>
    </property>
</bean>

<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
    <constructor-arg ref="mongoDbFactory" />
    <constructor-arg ref="converter" />
</bean>

<bean id="teaManAuthenticationSuccessHandler" class="com.teaman.authentication.handler.TeaManAuthenticationSuccessHandler"/>

的beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
 </beans>

faces-config.xml中

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>

    <locale-config>
        <default-locale>pl</default-locale>
    </locale-config>

    <resource-bundle>
        <base-name>com.teaman.locales.locale</base-name>
        <var>msg</var>
    </resource-bundle>
</application> 

<application>
    <message-bundle>com.teaman.ValidationMessages</message-bundle>
</application>

<lifecycle>
    <phase-listener>com.teaman.bean.LoginErrorPhaseListener</phase-listener>
</lifecycle>

的web.xml

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
        /WEB-INF/securityContext.xml
    </param-value>
</context-param>

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

<!-- Context listener responsible for loading spring applicationContext file -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
    <url-pattern>/javax.faces.resource/*</url-pattern>
</servlet-mapping>

ManagedBean

package com.teaman.bean;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.inject.Named;
import org.apache.log4j.Logger;

@Named(value = "teaManUserSessionBean")
@SessionScoped
public class TeaManUserSessionBean implements Serializable {

     ....
     public void initUserSession(ComponentSystemEvent e) {
           logger.debug("TeaManUserSession init...");
     }
     ....
}

也许有人已经遇到过同样的问题,可以帮助我们吗? :) 非常感谢每一个提示/注释/评论。

1 个答案:

答案 0 :(得分:0)

我建议您使用Spring库中的@Scope(“session”)而不是CDI中的@SessionScoped。您似乎与默认的Spring作用域(应用程序作用域)存在冲突,因此它不起作用。