Monday 16 January 2012

Liferay Spring integration

Getting the Spring context when running LifeRay

Spring context per WAR file 
First, update your web.xml file to load the context:

<web-app>
  <description>Financial Quotes Portlet Application</description>
 
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/YourSpringContextFile.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  .....
</web-app>
 Then as you can see from the web.xlm file, you are putting your spring context file in the WEB-INF directory of your WAR file.

Now, to get a bean, you do the following. I usually create a helper/utility class to help fetch beans, but here it is in a nutshell:

PortletContext context = getPortletContext();
WebApplicationContext springContext = (WebApplicationContext)   
                 context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
Object beanObject = springContext.getBean("yourBeanName");
 the beanObject should be cast to your Spring bean interface/class. Then you can use it. If you are getting the bean from a servlet, its pretty much the same except you use a ServletContext instead of a PortletContext.

Various Spring Contexts
Spring provides various contexts depending on how you are using spring: an application context (for apps such as stand-alone apps, ie desktop applications that don't run in a web container), web applicatoin contexts and portlet contexts.
The example given included updated the web.xml file, which is a web-level configuration file, not specific to portlets.
There is a PortletApplicationContext. Spring has a Portlet MVC framework that you can use with your JSR-168 portlets.
Where is this Spring file located? If its in a different web application, this can be problematic. For instance, if this file is in lferay's ROOT web application, it will be in a different classloader than your web application (assuming you are deploying a different WAR file).





Read also http://www.liferay.com/community/forums/-/message_boards/message/2134782

No comments:

Post a Comment