Spring
Here's a forum thread on Spring integration: http://www.opencms-forum.de/opencms-forum/viewthread?thread=1455
Spring Framework 2.0
Integrating the Spring Framework usually involves definition of a Web Application Context listener-class in web.xml. The Spring Framework already comes with such a listener, org.springframework.web.context.ContextLoaderListener.
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
It starts the Spring ApplicationContext/BeanFactory. It might be required to override the context-pararm contextConfigLocation to tell the listener where it can find the configuration file (the default param-value is /WEB-INF/applicationContext.xml).
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/application-context.xml</param-value> <description>Tells Spring where to find its configuration</description> </context-param>
The above example shows how to access an application-context.xml which is a classpath resource (deployed in WEB-INF/lib/*.jar or WEB-INF/classes).
Deployment Restriction
Defining a context listener makes it necessary that the listener class is available during startup of the Web Application. You must deploy the Spring Framework directly in WEB-INF/lib. It is impossible to deploy Spring as part of an OpenCms module because the listener class would not be available when the module is not yet published. The context listener definition would not find the class and prevent the whole OpenCms Web Application from starting up because the Spring JARs would not be in WEB-INF/lib yet but still only in the lib folder of your module in the VFS. On Tomcat, if a listener cannot be started there is an error in the catalina.out log file saying
SEVERE: Error listenerStart
Usually, the localhost_log contains more information including a stack trace.
A workaround for deploying Spring within a module would be to import and publish the module first and define the listener in a second step. The listener definition would have to be removed before uninstalling the module, otherwise the next start of OpenCms would fail.
Hibernate Session in View Pattern
If you want to use Hibernate along with Spring you can use the appropriate hibernate filter to manage sessions.
<filter> <filter-name>hibernateFilter</filter-name> <description> Creates sessions and transactions for each mapped request </description> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter>
Map the filter to the OpenCmsServlet.
<filter-mapping> <filter-name>hibernateFilter</filter-name> <servlet-name>OpenCmsServlet</servlet-name> </filter-mapping>
Each request to the OpenCms servlet will be surrounded by a Hibernate session.
This is somewhat suboptimal as the session is created even when it is not required. Your code which requires a session and transactions might not be executed at all. Nevertheless, the Open Session in View pattern is well-known and the easiest solution to the lazy initialization problem which occurs when lazy members of a Hibernate proxy object are accessed after its session has been closed.