@SessionScoped CDI bean is a different Instance wh

2019-02-18 02:12发布

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.

My 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);
    }
}

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

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

The WebApp is running on TomEE 1.7.4

UPDATE: I created a new project to test it, and the SessionScope works. I now need to find out what is wrong with my current project in order to fix it.

Facets:

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

Any ideas ?

3条回答
smile是对你的礼貌
2楼-- · 2019-02-18 02:31

We have @SessionScope annotation in both JSF & CDI. Please review whether the annotation you are using in your old project is from JSF or from CDI. Find more on the difference between the annotation from JSF & CDI

查看更多
forever°为你锁心
3楼-- · 2019-02-18 02:37

You are doing it right. That is, from CDI perspective, you made no mistake and what you want is perfectly legit and should work (assuming you solved the problem of multiple sessions, which you did).

I just tried this with my own piece of code and it works as expected. You can check it on GitHub. The sample is more or less identical to yours.

However, I am running Wildfly 10 and therefore Weld 2.3 which comes with it (Weld being a reference impl of CDI). While you are running TomEE which contains OpenWebBeans (another CDI implementation).

To me it seems like you either missed some TomEE/OWB specific configuration (unrealistic scenario) or, more likely, you found a bug. In any case, if I were you, I would try asking on their forums or creating an issue in their tracking system because, once again, there is imho nothing wrong with your bean/servlet setup.

查看更多
欢心
4楼-- · 2019-02-18 02:51

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)

查看更多
登录 后发表回答