Spring

(Difference between revisions)
Jump to: navigation, search
(information about context listener, deployment and hibernate)
(added information about hibernate open session in view filter)
Line 1: Line 1:
 
Here's a forum thread on Spring integration:  http://www.opencms-forum.de/opencms-forum/viewthread?thread=1455
 
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 <tt>listener-class</tt> in ''web.xml''. The Spring Framework already comes with such a listener, <tt>org.springframework.web.context.ContextLoaderListener</tt>.  
 
Integrating the Spring Framework usually involves definition of a Web Application Context <tt>listener-class</tt> in ''web.xml''. The Spring Framework already comes with such a listener, <tt>org.springframework.web.context.ContextLoaderListener</tt>.  
  
Line 7: Line 8:
 
  </listener>
 
  </listener>
  
It starts the Spring ApplicationContext/BeanFactory. It might be required to override the ''context-pararm'' <tt>contextConfigLocation</tt> to tell the listener where it can find the configuration file (the default is /WEB-INF/applicationContext.xml).  
+
It starts the Spring ApplicationContext/BeanFactory. It might be required to override the ''context-pararm'' <tt>contextConfigLocation</tt> to tell the listener where it can find the configuration file (the default ''param-value'' is /WEB-INF/applicationContext.xml).  
  
 
  <context-param>
 
  <context-param>
Line 15: Line 16:
 
  </context-param>
 
  </context-param>
  
== Deployment Restriction ==
+
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'').
Also note that defining a context listener makes it necessary to deploy the Spring JAR in <tt>WEB-INF/lib</tt>. It is impossible to deploy Spring as part of an OpenCms module because the listener definition prevents the OpenCms Web Application from starting up when the module is first deployed but not yet published (the Spring JARs would not be in WEB-INF/lib yet, but just in the lib folder of your module in the VFS). On Tomcat, if the listener cannot be started there is an error in the catalina.out log file saying  
+
 
 +
=== 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 <tt>WEB-INF/lib</tt>. 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
 
  SEVERE: Error listenerStart
  
 
Usually, the ''localhost_log'' contains more information including a stack trace.
 
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 [http://www.hibernate.org/43.html 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.

Revision as of 11:26, 5 December 2006

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.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox