Note that there are some explanatory texts on larger screens.

plurals
  1. POJavax.enterprise.context.SessionsScoped behaving like Singleton in SpringContainer with JSF as frontend
    text
    copied!<p>I've Web App with front End JSF/RichFaces, Middle Tier Spring and Hibernate for DB. When I deployed the app and accessed from my system and my friends access, My friend could continue whatever the operation I had done(object was behaving as singleton/ applicationcontext). Below is the code of my session class, web.xml, faces-config.xml and spring.xml. I guess due to same reason I believe viewscoped bean is also not behaving to my expectation. Could somebody help in this case.</p> <pre><code>package org.kp.business; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.enterprise.context.SessionScoped; import javax.inject.Named; @Named @SessionScoped public class OliHolder implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Map&lt;String, ArrayList&lt;OliData&gt;&gt; oliTypesHolder = new HashMap&lt;String, ArrayList&lt;OliData&gt;&gt;(); public void addOli(String oliType) { OliData oliDetails = new OliData(); oliDetails.setOliName(oliType); oliDetails.initData(oliType); ArrayList&lt;OliData&gt; tempHolder = null; if(!oliTypesHolder.containsKey(oliType)) { tempHolder = new ArrayList&lt;OliData&gt;(); tempHolder.add(oliDetails); oliTypesHolder.put(oliType, tempHolder); }else{ tempHolder = oliTypesHolder.get(oliType); tempHolder.add(oliDetails); } } public void deleOli(int num, String oliType) { ArrayList&lt;OliData&gt; list = oliTypesHolder.get(oliType); if(null != list &amp;&amp; list.size() &gt; 0){ if((list.size()&gt;num)){ list.remove(num); } } } public void resetOliHolder(){ oliTypesHolder.clear(); } public List&lt;OliData&gt; getOlisByType(String oliType){ ArrayList&lt;OliData&gt; tempHolder = oliTypesHolder.get(oliType); return tempHolder; } public void resetCurrentType(String selectedOli) { oliTypesHolder.remove(selectedOli); } public Map&lt;String, ArrayList&lt;OliData&gt;&gt; getOliTypesHolder() { return oliTypesHolder; } } </code></pre> <p>Face-config.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;faces-config 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/web-facesconfig_2_1.xsd" version="2.1"&gt; &lt;application&gt; &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p>web.xml</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;web-app version="3.0" 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/web-app_3_0.xsd"&gt; &lt;display-name&gt;EBMMaker&lt;/display-name&gt; &lt;!-- &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.html&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; --&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.faces&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;faces/index.xhtml&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;context-param&gt; &lt;param-name&gt;org.richfaces.skin&lt;/param-name&gt; &lt;param-value&gt;ruby&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/SpringConfig.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p>Spring.xml</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="org.kp"&gt;&lt;/context:component-scan&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="url" value="jdbc:mysql://localhost:3306/ebm" /&gt; &lt;property name="username" value="root" /&gt; &lt;property name="password" value="root" /&gt; &lt;/bean&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="packagesToScan" value="örg.kp.db.model" /&gt; &lt;property name="configLocation"&gt; &lt;value&gt; classpath:/org/kp/db/hibernate.cfg.xml &lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>Here is one class were I'm trying to acess bbOli Bean which is used to get data from database Since I'm creating manually the object, I'm acessing bbOli Bean using application context. I'm not sure the Scope of the OLIData object.</p> <pre><code>package org.kp.business; import java.util.ArrayList; import java.util.List; import javax.faces.context.FacesContext; import javax.servlet.ServletContext; import org.kp.db.model.TblEBMFieldDetails; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class OliData { BusinessOli bbOli; private String oliName; public OliData() { ApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext((ServletContext) FacesContext .getCurrentInstance().getExternalContext().getContext()); bbOli = (BusinessOli) ctx.getBean("bbOli"); } private String commentText; public String getCommentText() { return commentText; } public void setCommentText(String commentText) { this.commentText = commentText; } public String getOliName() { return oliName; } public void setOliName(String oliName) { this.oliName = oliName; } private List&lt;TblEBMFieldDetails&gt; dbEBMData; private List&lt;EBMDataForView&gt; renderEBMData; public List&lt;EBMDataForView&gt; getRenderEBMData() { return renderEBMData; } public void setRenderEBMData(List&lt;EBMDataForView&gt; renderEBMData) { this.renderEBMData = renderEBMData; } public List&lt;TblEBMFieldDetails&gt; getDbEBMData() { return dbEBMData; } public void setDbEBMData(List&lt;TblEBMFieldDetails&gt; dbEBMData) { this.dbEBMData = dbEBMData; } public void initData(String oliName) { this.dbEBMData = bbOli.getOliTypeSpfcData(oliName); renderEBMData = new ArrayList&lt;EBMDataForView&gt;(); for (TblEBMFieldDetails dt : dbEBMData) { EBMDataForView data = new EBMDataForView(); data.setCmplxFlg(dt.isCollectionFlg()); if (dt.isCollectionFlg() &amp;&amp; null != dt.getValues()) { data.setFieldCollectionValue(dt.getValues().split(",")); System.out.println("Hey I'm: "+data.getFieldCollectionValue().length); } data.setFieldDefaultValue(dt.getDefaultValue()); data.setFieldName(dt.getFieldName()); data.setRequiredFld(dt.isReqFlg()); renderEBMData.add(data); } } } </code></pre> <p>After Change</p> <pre><code>import org.springframework.beans.factory.annotation.Autowired; @ManagedBean(name = "bbOliHolder") @ViewScoped public class OliHolderBacking implements Serializable{ private static final long serialVersionUID = 1L; private String selectedTab; @Autowired BusinessOli bbOli; public BusinessOli getBbOli() { return bbOli; } public void setBbOli(BusinessOli bbOli) { this.bbOli = bbOli; } @Autowired OliHolder oliHolder; private String[] allOliTypes = null; @PostConstruct public void init() { allOliTypes = getBbOli().getAllOlisFromDB(); olis = new ArrayList&lt;OliData&gt;(); } } </code></pre> <p>Service Layer Bean</p> <pre><code>import java.util.List; import org.kp.db.dao.EBMDetailDao; import org.kp.db.dao.impl.OlisImpl; import org.kp.db.model.TblEBMFieldDetails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service("bbOli") @Scope("prototype") public class BusinessOli{ @Autowired OlisImpl olisImpl; @Autowired EBMDetailDao ebmData; public String[] getAllOlisFromDB() { String[] str = olisImpl.getAllOlis(); return str; } } </code></pre> <p>However The Autowiring in BusinessOli were working earlier before the change</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload