@SessionScoped CDI bean is another instance when injected

My configuration is a bean that I embed in my code wherever I need it. However, when I enter, I get a new bean instance instead of a 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 introduced into different classes, all occurrences are different instances. When annotating a bean using @ApplicationScoped, this is the same instance.

I need the bean to be @SessionScoped, as each user must have their own configuration.

WebApp runs on TomEE 1.7.4

UPDATE: I created a new project to test it, and SessionScope is working. Now I 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 by 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?

+4
3

, :

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

, , , ( , )

+2

. , CDI, , , , ( , ).

, , . GitHub. .

, Wildfly 10 , , Weld 2.3, (Weld CDI). TomEE, OpenWebBeans ( CDI).

, TomEE/OWB ( ), , , . , , , , , imho bean/.

+1

@SessionScope JSF, CDI. , , , JSF CDI. JSF CDI

0
source

Source: https://habr.com/ru/post/1649553/


All Articles