@SessionScoped CDI bean在注入时是不同的实例 [英] @SessionScoped CDI bean is a different Instance when injected

查看:47
本文介绍了@SessionScoped CDI bean在注入时是不同的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置是我需要时在代码中注入的Bean。但是,在注入时,我从会话中获得了一个新的bean实例,而不是一个实例。

My config is a bean that I inject in my code wherever I need it. However, when injected, I get a new instance of the bean instead of the one from the session.

我的bean:

@Named
@SessionScoped
public class TestModel implements Serializable {

    private static final long serialVersionUID = 4873651498076344849L;

    private String version;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void changeVersion() {       
        this.version = "Version 2";
        System.out.println("New version : " + version + ", Object : " + this);
    }
}

当注入不同的类时,所有事件都是不同的实例。
用@ApplicationScoped注释bean时,它是同一实例。

When injected in different classes, all occurences are different instances. When annotating the bean with @ApplicationScoped, it is the same instance.

我确实需要将bean设为@SessionScoped,因为每个用户都应该有自己的配置

I do need the bean to be @SessionScoped since every user should have his own config.

WebApp正在TomEE 1.7.4上运行

The WebApp is running on TomEE 1.7.4

更新:我创建了一个新项目测试它,SessionScope起作用。我现在需要找出我当前项目的问题才能修复它。

Facets:


  • CDI 1.0

  • 动态Web模块3.0

  • Java 1.8

  • JSF 2.2(来自TomEE的MyFaces体现)

  • JPA 2.1

  • CDI 1.0
  • Dynamic Web Module 3.0
  • Java 1.8
  • JSF 2.2 (MyFaces impl from TomEE)
  • JPA 2.1

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Project</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>omega</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
  </context-param>
  <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>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

faces-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
<faces-config
    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"
    version="2.2">

</faces-config>

beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_1.xsd"
       version="1.1" bean-discovery-mode="annotated">           
</beans>

有什么想法吗?

推荐答案

看起来您的测试无法正常工作:

Looks like your test doesn't work:

testModel object = model.TestModel@689a6064
New version : Version 2, Object : model.TestModel@61606aa6

因此,您更新了一个实例与链接到该会话的会话不同(另一个请求未重用我要说的同一会话)

So you update an instance which is not the same as the one linked to the session (another request not reusing the same session I'd say)

这篇关于@SessionScoped CDI bean在注入时是不同的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆