Spring

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(added information about hibernate open session in view filter)
(Removed link to opencms-forum.de)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Here's a forum thread on Spring integration: http://www.opencms-forum.de/opencms-forum/viewthread?thread=1455
+
 
 +
A basic Spring/OpenCms 'getting-started' project: http://www.sendspace.com/file/x2sipm
  
 
== Spring Framework 2.0 ==  
 
== Spring Framework 2.0 ==  
Line 19: Line 20:
  
 
=== Deployment Restriction ===
 
=== 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  
+
The Spring Framework should be deployed directly to <tt>WEB-INF/lib</tt>. Defining a context listener requires the listener class to be available during startup of the Web Application. It is inconvenient to deploy listener classes as part of an OpenCms module because all included classes are unavailable to the WebappClassLoader prior to publishing the module. This causes listener initializations to fail and leads to a bootstrap problem. With Tomcat, Web Application startup fails if a single listener fails to initialize. There is an error in the ''catalina.out'' log file saying  
  
 
  SEVERE: Error listenerStart
 
  SEVERE: Error listenerStart
 +
SEVERE: Context [...] startup failed due to previous errors
  
 
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.
+
The obvious workaround for deploying listeners within a module would be to import and publish the module first and define the listener in a second step. Removal of the module requires the reverse order.
 +
 
 +
=== Accessing the Application Context ===
 +
It might be a good idea to supply your own application context class and tell the listener to start it. Your class can keep a static reference of the initialized context instance. You can do that by subclassing ''org.springframework.web.context.support.XmlWebApplicationContext''.
 +
 
 +
The application context is configured with another ''context-param''. It is read by ''ContextLoaderListener''.
 +
 
 +
<context-param>
 +
  <param-name>contextClass</param-name>
 +
  <param-value>com.acme.spring.AcmeSpringContext</param-value>
 +
  <description>Tells the Spring ContextListener which class to use</description>
 +
</context-param>
 +
 
 +
The custom ''contextClass'' must be available at context start and thus may not be part of a module.
  
 
=== Hibernate Session in View Pattern ===
 
=== Hibernate Session in View Pattern ===

Latest revision as of 09:38, 14 May 2014

A basic Spring/OpenCms 'getting-started' project: http://www.sendspace.com/file/x2sipm

Contents

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

The Spring Framework should be deployed directly to WEB-INF/lib. Defining a context listener requires the listener class to be available during startup of the Web Application. It is inconvenient to deploy listener classes as part of an OpenCms module because all included classes are unavailable to the WebappClassLoader prior to publishing the module. This causes listener initializations to fail and leads to a bootstrap problem. With Tomcat, Web Application startup fails if a single listener fails to initialize. There is an error in the catalina.out log file saying

SEVERE: Error listenerStart
SEVERE: Context [...] startup failed due to previous errors

Usually, the localhost_log contains more information including a stack trace.

The obvious workaround for deploying listeners within a module would be to import and publish the module first and define the listener in a second step. Removal of the module requires the reverse order.

Accessing the Application Context

It might be a good idea to supply your own application context class and tell the listener to start it. Your class can keep a static reference of the initialized context instance. You can do that by subclassing org.springframework.web.context.support.XmlWebApplicationContext.

The application context is configured with another context-param. It is read by ContextLoaderListener.

<context-param>
  <param-name>contextClass</param-name>
  <param-value>com.acme.spring.AcmeSpringContext</param-value>
  <description>Tells the Spring ContextListener which class to use</description>
</context-param>

The custom contextClass must be available at context start and thus may not be part of a module.

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