Back Forum Reply New

scope=quot;sessionquot;

I'm trying to run some simple example that use scope=quot;sessionquot;. But it doesnt work. I think with this type of scope each user session create a new bean, but this not work in my example. The controller uses the same bean to all application. What is the problem? I'm using RC2.

Thanks to all

Adilson

lt;bgt;web.xmllt;/bgt;

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;web-app id=quot;WebApp_IDquot; version=quot;2.4quot; xmlns=quot;xml/ns/j2eequot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xsi:schemaLocation=quot;xml/ns/j2ee xml/ns/j2ee/web-app_2_4.xsdquot;gt;
lt;display-namegt; testespring
lt;/display-namegt;
lt;context-paramgt;
lt;param-namegt;contextConfigLocationlt;/param-namegt;
lt;param-valuegt;
/WEB-INF/applicationContext.xml
lt;/param-valuegt;
lt;/context-paramgt;

lt;listenergt;
lt;listener-classgt;org..web.context.scope.RequestContextListenerlt;/listener-classgt;
lt;/listenergt;

lt;listenergt;
lt;listener-classgt;
org..web.context.ContextLoaderListener
lt;/listener-classgt;
lt;/listenergt;

lt;servletgt;
lt;servlet-namegt;testespringlt;/servlet-namegt;
lt;servlet-classgt;
org..web.servlet.DispatcherServlet
lt;/servlet-classgt;
lt;load-on-startupgt;2lt;/load-on-startupgt;
lt;/servletgt;

lt;servlet-mappinggt;
lt;servlet-namegt;testespringlt;/servlet-namegt;
lt;ucl-patterngt;*.tstlt;/ucl-patterngt;
lt;/servlet-mappinggt;lt;welcome-file-listgt;
lt;welcome-filegt;index.jsplt;/welcome-filegt;
lt;/welcome-file-listgt;
lt;/web-appgt;
lt;bgt;applicationContext.xmllt;/bgt;

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans
xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xmlns:aop=quot;schema/aopquot;
xsi:schemaLocation=quot;schema/beans schema/beans/spring-beans.xsd
schema/aop schema/aop/spring-aop.xsdquot;
gt;
lt;bean id=quot;objetoSessaoquot; class=quot;ObjetoSessaoquot; scope=quot;sessionquot;gt;
lt;aop:scoped-proxy/gt;
lt;/beangt;

lt;/beansgt;
lt;bgt;testespring-servlet.xmllt;/bgt;

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans
xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xmlns:aop=quot;schema/aopquot;
xsi:schemaLocation=quot;schema/beans schema/beans/spring-beans.xsd
schema/aop schema/aop/spring-aop.xsdquot;
gt;

lt;bean name=quot;/hello.tstquot; class = quot;DependeSessaoquot;gt;
lt;property name=quot;objetoSessaoquot; ref=quot;objetoSessaoquot;/gt;
lt;/beangt;

lt;/beansgt;
lt;bgt;DependeSessao.javalt;/bgt;

Code:
public class DependeSessao extends AbstractController {

private ObjetoSessao objetoSessao;

public ObjetoSessao getObjetoSessao() {
return objetoSessao;
}

public void setObjetoSessao(ObjetoSessao objetoSessao) {
this.objetoSessao = objetoSessao;
}

@Override
protected ModelAndView handleRequestInternal(fromServletRequest arg0,
fromServletResponse arg1) throws Exception {

objetoSessao.valor++;
HashMap m = new HashMap();
m.put(quot;valorquot;, objetoSessao.valor);

return new ModelAndView(quot;index.jspquot;, m);
}

}Isn't quot;singleton=falsequot; what you are looking for?

In Spring 2.0 that is a new attribute scope. I think this is the correct.

But, if I use singleton=quot;falsequot;, like this:Code:
lt;bean id=quot;objetoSessaoquot; class=quot;ObjetoSessaoquot; singleton=quot;falsequot;/gt;
lt;bean name=quot;/hello.tstquot; class = quot;DependeSessaoquot;gt;  lt;property name=quot;objetoSessaoquot; ref=quot;objetoSessaoquot;/gt;
lt;/beangt;
why this create just 1 time the quot;objetoSessaoquot;?

I tested putting  lt;igt;System.out.println(quot;creating object...quot;)lt;/igt; at constructor, and print it just 1 time !!!!!!Where is my wrong?

Adilson

i'm also new to spring, so dont bother with me

thinking a little bit, try to set scope=quot;sessionquot;  or singleton=false on the bean named quot;/hello.tstquot; instead of objetoSessao.

thnking like spring, it would just call objectoSessao when creating ref to DependeSessao. I dont know...

Hope i could help... Thus im learning too.

I tested this, but I dont want this. I like that the object quot;DependeSessaoquot; must created 1 time, and the quot;ObjetoSessaoquot; depends on the request.

Adilson

and what error do you get when using scope?
When I'm trying to use a bean from jsf I get

Code:
javax.el.ELException: /view.xhtml:
Error creating bean with name 'abean': Scope 'session' is not active;
nested exception is java.lang.IllegalStateException:
No thread-bound request: use RequestContextFilter
And of course I have

Code:
lt;listenergt;
lt;listener-classgt;
org..web.context.scope.RequestContextListener
lt;/listener-classgt;
lt;/listenergt;There arent message errors. But the ObjetoSessao creates just 1 time.
¥
Back Forum Reply New