Project

General

Profile

Sample-vaadin-spring-hibernate » History » Version 16

Henning Blohm, 22.04.2019 23:52

1 1 Henning Blohm
h1. A sample using Vaadin with Hibernate JPA and Spring on Z2
2
3
This sample is similar to [[Sample-spring-hibernate]] but differs (or rather extends) in that it show cases
4
the use of the "Vaadin":http://www.vaadin.com user interface toolkit in conjunction with Spring implemented annotation based
5
dependency injection over Z2 modularity. 
6
7
As Spring is used throughout - in all modules - this is another practical application of [[How to Spring]].
8
9 8 Henning Blohm
Moreover this sample illustrates modularization of Vaadin user interfaces with Z2 using extension points as described in [[Vaadin add-on]].
10
11 1 Henning Blohm
This sample is stored in "z2-samples.vaadin-spring-hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate.
12
13
h2. Prerequisites
14
15 13 Henning Blohm
{{include(Java Version Requirements)}}
16 12 Henning Blohm
17 1 Henning Blohm
You need to run Java DB as network server on localhost. This is explained next.
18
19
The application will create a database "z2-samples"
20
21
{{include(How to run Java db)}}
22
23
24
h2. Run it
25
26 15 Henning Blohm
If you have the database, the fastest way to verify whether it runs is:
27 1 Henning Blohm
28 15 Henning Blohm
{{include(Install_sample_prefix)}}
29 1 Henning Blohm
30 15 Henning Blohm
Check out the sample
31 1 Henning Blohm
32 15 Henning Blohm
<pre><code class="ruby">
33 16 Henning Blohm
git clone -b v2.7 http://git.z2-environment.net/z2-samples.vaadin-spring-hibernate
34 1 Henning Blohm
</code></pre>
35 15 Henning Blohm
36
{{include(Install_sample_postfix)}}
37 1 Henning Blohm
38
When running, go to http://localhost:8080/vaadin-spring-hibernate. You should see this:
39
40
!vaadin-spring-hibernate.png!
41
42
h2. Details
43
44 3 Henning Blohm
As in the other samples we have a re-use domain module. That is a recurring theme for many good reasons. In this case, the domain module "com.zfabrik.samples.vaadin-spring-hibernate.domain":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.domain is essentially like the similarly named module of [[Sample-spring-hibernate]]. The only difference is some more data access methods in the "ThingyRepository":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.domain/java/src.api/com/zfabrik/samples/vaadin_spring_hibernate/thingies/ThingyRepository.java.
45 1 Henning Blohm
46 3 Henning Blohm
The Vaadin Web application is defined in the module "com.zfabrik.samples.vaadin-spring-hibernate.web":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.web. It has the usual Spring application context in "web/WebContent/WEB-INF/applicationContext.xml":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/web/WebContent/WEB-INF/applicationContext.xml that imports the thingy repository:
47 1 Henning Blohm
48 3 Henning Blohm
<pre><code class="xml">
49
        <!-- import external components -->
50
        <bean id="thingyRepository" class="com.zfabrik.springframework.ComponentFactoryBean">
51
                <property name="componentName" value="com.zfabrik.samples.vaadin-spring-hibernate.domain/repository" />
52
                <property name="className" value="com.zfabrik.samples.vaadin_spring_hibernate.thingies.ThingyRepository" />
53
        </bean>
54
</code></pre>
55
56 5 Henning Blohm
One particularity of using Vaadin in a modular environment is that Vaadin loads it's application class using the class loader that loaded the Vaadin classes (rather than the current thread's context class loader - as would be advised normally). When the Web application module is separated from the Vaadin module this can obviously not work too well (see also "Enhancement #9809":http://dev.vaadin.com/ticket/9809). To fix that you can tell Vaadin to use a custom class loader implementation (that - again - is loaded with the Vaadin class loader). That's why we declare the Vaadin servlet in "web/WebContent/WEB-INF/web.xml":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/web/WebContent/WEB-INF/web.xml list this:
57
58
<pre><code class="xml">
59
<!-- Vaadin -->
60
<servlet>
61
  <servlet-name>VaadinServlet</servlet-name>
62
  <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
63
  <init-param>
64
    <param-name>application</param-name>
65
    <param-value>com.zfabrik.samples.impl.vaadin_spring_hibernate.ApplicationImpl</param-value>
66
  </init-param>
67 1 Henning Blohm
  <!-- this because Vaadin doesn't use the thread's context class loader by default -->
68 8 Henning Blohm
  <init-param>
69 5 Henning Blohm
    <param-name>ClassLoader</param-name>
70
    <param-value>com.zfabrik.vaadin.ContextClassLoader</param-value>
71
  </init-param>*
72
</servlet>
73
</code></pre>
74
75 3 Henning Blohm
The Vaadin application class "ApplicationImpl":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate/ApplicationImpl.java constructs a simple view hierarchy that containes a table view based on a "lazy query container add-on":https://vaadin.com/directory#addon/lazy-query-container data model that is fed from the domain module. To do so the corresponding query implementation (in lazy query container speak) has the repository injected:
76
77
<pre><code class="java">
78
@Configurable
79
public class ThingiesQuery implements Query {
80
	@Autowired
81
	private ThingyRepository repository;
82
	private Integer size;
83
	private boolean asc;
84
85
// ...
86
}
87
</code></pre>
88 5 Henning Blohm
89
Please browse the actual UI code at "com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate. It pretty much speaks for itself.
90 4 Henning Blohm
91 10 Henning Blohm
h3. Transaction Management
92
93 4 Henning Blohm
One important note on transaction management: In this example, transaction boundaries are enforced by a servlet filter once more. It is implemented in "TransactionFilter":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate/util/TransactionFilter.java and essentially looks like this:
94
95
<pre><code class="java">
96
public class TransactionFilter implements Filter {
97
98
99
        @Transactional
100
        @Override
101
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
102
                chain.doFilter(request, response);
103
        }
104
105
}
106
</code></pre>
107
108
In other words, the only thing it really does is to make sure Spring spans a transaction (using the underlying JTA implementation) along the request. It is tempting to implement transaction demarcation in the Vaadin Application class. However, we cannot use the <code>@Transactional</code> annotation as there is no single request spanning method and using the JTA UserTransaction object is unfortunately not sufficient to "remote control" Spring's JTA wrappers. So the easiest work around is to use Spring TX demarcation right away (see also [[how to transaction management]]).
109 10 Henning Blohm
110
h3. Extensibility
111 6 Henning Blohm
112 7 Henning Blohm
Furthermore this sample illustrates the use of the *ExtensionComponentsUtil* ("javadoc":http://www.z2-environment.net/javadoc/com.zfabrik.vaadin!2Fjava/api/com/zfabrik/vaadin/ExtensionComponentsUtil.html) as described in detail at [[Vaadin_Add-on]]. This utility allows to conveniently modularize the user interface implementation by providing and retrieving Vaadin user interface component implementations across modules.
113 6 Henning Blohm
114 11 Henning Blohm
In the specific case, the module "com.zfabrik.samples.vaadin-spring-hibernate.extension":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.extension provides an extension component "somethingAboutThingies":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.extension/somethingAboutThingies.properties that is added as the button "About these..." to the button toolbar of the main interface. 
115
116
The component declaration makes sure the implementation can be found and (important!) that the module's Spring application context is loaded before providing the component implementation
117
118
<pre><code class="bash">
119
#
120
# A Vaadin UI extension 
121
#
122
com.zfabrik.component.type=com.vaadin.ui.Component
123
124
#
125
# Coordinates:
126
#
127
com.zfabrik.vaadin.extension.point=com.zfabrik.samples.vaadin_spring_hibernate.actions
128
com.zfabrik.vaadin.extension.order=100
129
130
#
131
# The implementation class
132
#
133
component.className=com.zfabrik.samples.impl.vaadin_spring_hibernate.ext.SomethingAboutThingies
134
135
#
136
# make sure the app context is loaded first
137
#
138
com.zfabrik.component.dependencies=\
139
	 com.zfabrik.samples.vaadin-spring-hibernate.extension/applicationContext
140
</code></pre>
141
142
The user interface that assembles consumes this extension is at "ThingiesView":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate/ThingiesView.java and adds extensions like this:
143
144
<pre><code class="java">
145
// find extensions and add them to the button row
146
for (Component c : ExtensionComponentsUtil.getComponentyByExtensionForApplication(application,"com.zfabrik.samples.vaadin_spring_hibernate.actions")) {
147
    // and add them to this.
148
    buttons.addComponent(c);
149
}
150
</code></pre>