4
votes

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
I guess that the problem is a session scope. Because you might have modified a different Config (from another session). Please change it to Singleton or ApplicationScoped and check if problem still exists.G. Demecki
Glad it worked but since Singleton and ApplicationScoped are basically the same, how did you solve your problem, when every user shall have his own config?G. Demecki
You are right. I tried with two simultaneous sessions, and the values updated for every user. So back to square one. On session scoped, the value does not get updated in the servlet :-(Tim
But to be honest: this doesn't make sense. SessionScoped should work just fine. Please make sure that during your tests you're using the same HTTP session for both requests (request.getSession().getId() has to be the same).G. Demecki
If the session id is different each time, your problem lies there. Find out why the session is new/different each time...Kukeltje

3 Answers

2
votes

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)

1
votes

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.

0
votes

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