Project

General

Profile

How to Spring » History » Version 19

Henning Blohm, 06.09.2012 18:12

1 16 Henning Blohm
h1. How to Use the Spring Framework in Z2
2
3 18 Henning Blohm
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 Howto. However it is required that you are familiar with the Spring framework as such.
4 2 Henning Blohm
5
h2. Pre-Requisites
6
7
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
8
9 17 Henning Blohm
<pre><code class="python">
10 15 Henning Blohm
com.zfabrik.systemStates.participation=com.zfabrik.boot.main/sysrepo_up
11 3 Henning Blohm
12
# git stored component repository
13
com.zfabrik.component.type=com.zfabrik.gitcr
14
15
# true <=> optional repository. If gitcr.uri is invalid, then this gitcr will be ignore silently  
16
gitcr.optional=true
17
18
# this can also point to a remote repository like 
19
# ssh://myserver/some/git/repo
20
gitcr.uri=http://git.z2-environment.net/z2-addons.spring
21
22
# the git branch to use (e.g. 'master')
23
gitcr.branch=master
24
</code></pre>
25 15 Henning Blohm
26 3 Henning Blohm
For your own system, you may need to adapt the repository URL and the branch selection accordingly.
27
28
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]].
29
30
31 9 Henning Blohm
When you added that repository the following modules are available:
32 4 Henning Blohm
33
* org.springframework.orm
34
* org.springframework.security
35
* org.springframework.foundation   
36
* org.springframework.transaction
37
* org.springframework.jdbc         
38
* org.springframework.web
39
* com.zfabrik.springframework      
40
* com.zfabrik.springframework.web  
41
42 1 Henning Blohm
43
With the exception of those starting with "com.zfabrik", these do, more or less, correspond to the typical Spring modules found out there. 
44
45 18 Henning Blohm
Before you get frustrated by the amount of details in this Howto, please remember that there is practical samples available to help you check how things can really be assembled to work nicely:
46 10 Henning Blohm
47
* z2-samples.jta-spring (LINK)
48
49 4 Henning Blohm
h2. Using Spring in Web Applications
50 2 Henning Blohm
51
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. 
52 5 Henning Blohm
53
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 
54
context initialized as the Web app is started.
55
56
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*:
57
58 17 Henning Blohm
<pre><code class="python">
59 5 Henning Blohm
java.privateReferences=\
60
	com.zfabrik.springframework.web,\
61
	org.springframework.foundation,\
62
	org.springframework.web
63
64 11 Henning Blohm
</code></pre>
65 5 Henning Blohm
66
The reference to *com.zfabrik.springframework.web* is not strictly needed but adds the following capabilities:
67
68 7 Henning Blohm
* You implicitly get *com.zfabrik.springframework*, i.e. the integration features described below
69 5 Henning Blohm
70 7 Henning Blohm
* You can use a parent application context to your Web application application context as easily as you would hope (see below).
71
72
73 19 Henning Blohm
h2. Using Spring Context Support
74 7 Henning Blohm
75 19 Henning Blohm
The module *org.springframework.foundation* holds a Java component *org.springframework.foundation/context.support* that is not meant to be referenced but to be _included_.
76
Including a Java component means that it's resources (class files, jars,...) will be copied into the including scope rather than referenced via class loader delegation.
77 7 Henning Blohm
78 19 Henning Blohm
The difference is that in that case, all the includer sees is also be seen by the types in the included component. And that is exactly what context.support is about: It holds adapter classes that effectively have dangling dependencies on other prominent libraries. Say for example we are talking about the Lucene search engine library. If your Java component references the Lucene libs (which are not included with Z2 by default), and say you want to use the Spring adapter types, then include context.support by adding (i.e. customizing correspondingly) the private include:
79
80
<pre><code class="python">
81
java.privateIncludes=\
82
   org.springframework.foundation/context.support
83
</code></pre>
84
85
Typically it makes most sense to only use _private includes_. If you include into the API, which is possible, other modules that reference your API will find the included types
86
of your API with preference of their includes (as part of the parent-first delegation principle). So, they may not be able to repeat the same include pattern described above. 
87
88 2 Henning Blohm
h2. Using Spring in Re-use Modules
89 19 Henning Blohm
90
91 2 Henning Blohm
92
h2. Using Spring's Aspect/J Configuration
93 7 Henning Blohm
94
h2. Using a Parent Application Context in a Web Application Application Context
95
96
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/). 
97 8 Henning Blohm
98 7 Henning Blohm
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.