How to Spring » History » Revision 7
Revision 6 (Henning Blohm, 05.09.2012 17:59) → Revision 7/62 (Henning Blohm, 05.09.2012 18:09)
h1. How to Use the Spring Framework in Z2 There is actually nothing sooo particular about using Spring in Z2. But when knowing how Z2 modularity works, there is much to gain by spending a few minutes reading this How-To. However it is required that you are familiar with the Spring framework as such. h2. Pre-Requisites In order to have the Spring libraries available you need to add the repository _z2-addons.spring_ to your environment. Some samples, for example the z2-samples.jta-spring sample explained in [[How to TransactionManager]] does so. To use the master branch version, add a *springRepository.properties* component descriptor to your *environment* module saying <pre> com.zfabrik.systemStates.participation=com.zfabrik.boot.main/sysrepo_up # git stored component repository com.zfabrik.component.type=com.zfabrik.gitcr # true <=> optional repository. If gitcr.uri is invalid, then this gitcr will be ignore silently gitcr.optional=true # this can also point to a remote repository like # ssh://myserver/some/git/repo gitcr.uri=http://git.z2-environment.net/z2-addons.spring # the git branch to use (e.g. 'master') gitcr.branch=master </pre> For your own system, you may need to adapt the repository URL and the branch selection accordingly. If that sounds like meaningless gibberish to you - sorry please consult the documentation at http://www.z2-environment.eu/v20doc and go back to [[First_steps_with_z2]]. When you added that repository the following modules are available: * org.springframework.orm * org.springframework.security * org.springframework.foundation * org.springframework.transaction * org.springframework.jdbc * org.springframework.web * com.zfabrik.springframework * com.zfabrik.springframework.web With the exception of those starting with "com.zfabrik", these do, more or less, correspond to the typical Spring modules found out there. h2. Using Spring in Web Applications This is the simplest and really just the standard case. If you do not strive for re-use across modules that use Spring, then there is not much to worry about. As usual, you define an application context in the WEB-INF folder of the Web application and set up a context listener in WEB-INF/web.xml to have the application context initialized as the Web app is started. In order to have the minimal set of dependencies satisfied - i.e. not assuming you want to use the (very cool) AspectJ based Spring configuration, you should _add_ (i.e. augment whatever refs you already have) the following references to *java/z.properties*: <pre> java.privateReferences=\ com.zfabrik.springframework.web,\ org.springframework.foundation,\ org.springframework.web </pre> The reference to *com.zfabrik.springframework.web* is not strictly needed but adds the following capabilities: * You implicitly get *com.zfabrik.springframework*, i.e. the integration features described below * You can use a parent application context to your Web application application context as easily as you would hope (see below). h2. Using Spring in Re-use Modules h2. Using Spring's Aspect/J Configuration h2. Using a Parent Application Context in a Web Application Application Context Sometimes, later, when you find that your module has a web app but in addition you want to expose services, from the very same module, i.e. when you have spring configured objects in the web app but also a classpath defined application context that should serve as a _parent application context_ to the one of the web app (admittedly an advanced case), then you will find that that is slightly tricky to achieve (see e.g. http://blog.springsource.org/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/). The class *com.zfabrik.springframework.web.ComponentParentContextContextLoaderListener* is a drop-in replacement for Spring's ContextLoaderListener implementation that simplifies that use-case as explained in http://z2spring.z2-environment.net/javadoc/com.zfabrik.springframework.web!2Fjava/api/com/zfabrik/springframework/web/ComponentParentContextContextLoaderListener.html.