From: Cyril C. (JIRA) <ji...@an...> - 2005-11-08 13:16:13
|
[ http://jira.andromda.org/browse/SPRING-131?page=comments#action_12280 ] Cyril Combe commented on SPRING-131: ------------------------------------ Here is a possible strategy for implementing stateful services with Spring only. Feel free to comment as I am not an experienced Spring user. Let's say that we have a stateful service <my.package.MyStatefulService>. The applicationContext entry for this service should look like this: <!-- MyStatefulService Service Proxy with inner MyStatefulService Service Implementation --> <bean id="myStatefulService" class="org.springframework.aop.framework.ProxyFactoryBean" singleton="false"> <property name="target"> <bean class="my.package.MyStatefulServiceImpl" singleton="false"> <property name="myDao"><ref bean="myDao"/></property> </bean> </property> <property name="proxyInterfaces"> <value>my.package.MyStatefulService</value> </property> <property name="interceptorNames"> <list> <value>serviceTransactionInterceptor</value> <value>hibernateInterceptor</value> </list> </property> </bean> I think that no change are required in the ServiceLocator class, but as we have set the singleton flag to <false> for both the ProxyFactoryBean and his target, it should return a new instance for each call. Now the service should be cached on the client side. For instance, if dealing with the BPM4Struts cartridge for the web layer, a dependency from a controller to this service should produce this method in the base controller: /** * Returns a reference to the myStatefulService back-end service. */ protected final my.package.MyStatefulService getMyStatefulService(HttpServletRequest request) throws java.lang.Exception { javax.servlet.http.HttpSession session = request.getSession(); my.package.MyStatefulService service = (my.package.MyStatefulService)session.getAttribute("myStatefulService"); if (service == null) { service = my.package.ServiceLocator.instance().getMyStatefulService(); session.setAttribute("myStatefulService", service); } return service; } If the service is not referenced in the session, we ask for a new instance to the ServiceLocator and we put it in the HttpSession object. > Allow the creation of stateful services > --------------------------------------- > > Key: SPRING-131 > URL: http://jira.andromda.org/browse/SPRING-131 > Project: Spring Cartridge > Type: Improvement > Reporter: Cyril Combe > Assignee: Chad Brandon > > If a <<Service>> stereotyped classifier has instance scoped attributes, it should be implemented as a stateful service (a stateful session bean if EJBs are enabled). |