cancel
Showing results for 
Search instead for 
Did you mean: 

Scoped WebApplication Beans

nyronian
Champ in-the-making
Champ in-the-making
I would like to be able to have scoped beans for Alfresco WebApplication Context from a servlet (not faces). 

In order to get it to work I had to do the following:

In the web.xml, add the following:

<web-app>
  …
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  …
</web-app>

In my module-context.xml, add the following (Note I added 2.0 to my DOCTYPE):

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

   <bean id="PageRequestState" class="com.tlhc.wc.website.PageRequestState" scope="request" >
    …
   </bean>
</beans>

and get the bean with the following:

      WebApplicationContext wc = RequestContextUtils.getWebApplicationContext(request, sc);
      PageRequestState prs =  (PageRequestState)wc.getBean(PageRequestState.PAGE_REQUEST_STATE);

It works but I am surprised Alfresco does not already support it.  Am I missing something?  I realized you can do it in the Faces Container, but I need to be able to scope my beans it from a servlet.
2 REPLIES 2

daniel_gradecak
Confirmed Champ
Confirmed Champ
nobody from Alfresco can answer this question?

daniel_gradecak
Confirmed Champ
Confirmed Champ
Just to share how I do it.

Before using the scoped proxy you need to have the proxy bound to the thread. The Spring's servlet listener or the filter does exactly this :
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpReq));

and it works in alfresco where you can use a bean with aop.
<bean id="jibe.repo.SearchResultsProvider" class="com.icodix.jibe.repo.dataprovider.SearchResultsGridProvider" parent="jibe.GridDataProvider"
scope="session">
<aop:scoped-proxy/>
</bean>