Hello,
At first, sorry for my poor english, perhaps it would be difficult to understand my need...
In a web application, I would like that spring session beans be available to a servlet. To make that possible, the only way I've found is to instanciate a HttpSessionListener where I call WebApplicationContext.getBean(theBeanName) then manually add the bean in the session.
first question : Why do I need manually add the bean in the session, when that bean is defined as a session bean in applicationContext.xml ? Why the method getBean do not do that Job ? Is there another way to force the instanciation of a session bean ?
second question : is the use of HttpSessionListener is the only way to make session beans available to another servlet ? Is there another way where Spring will construct session bean before the servlet start ?
here some peace of declaration :
=== web.xml
<!-- Spring integration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListe ner
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestCon textListener
</listener-class>
</listener>
<!-- my custom HttpSessionListener where I create session bean -->
<listener>
<listener-class>essais.MySessionListener</listener-class>
</listener>
<!-- the servlet where I need Spring session bean -->
<servlet>
<servlet-name>
com.metaparadigm.jsonrpc.JSONRPCServlet
</servlet-name>
<servlet-class>
com.metaparadigm.jsonrpc.JSONRPCServlet
</servlet-class>
<init-param>
<param-name>auto-session-bridge</param-name>
<param-value>1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>
com.metaparadigm.jsonrpc.JSONRPCServlet
</servlet-name>
<url-pattern>/JSON-RPC</url-pattern>
</servlet-mapping>
=== applicationContext.xml
<!-- the session bean which I would like to be ready before the servlet start -->
<bean id="JSONRPCBridge" scope="session" lazy-init="false" class="essais.MyJSONRPCBridge">
</bean>
=== MySessionListener.java
public synchronized void sessionCreated(HttpSessionEvent arg0)
{
...
HttpSession session = arg0.getSession();
WebApplicationContext webAppContext = WebApplicationContextUtils.getRequiredWebApplicati onContext(session.getServletContext());
String beanName = "JSONRPCBridge" ;
MyJSONRPCBridge bean = (MyJSONRPCBridge) webAppContext.getBean(beanName);
session.setAttribute(beanName, bean);
...
}


Reply With Quote


