Project

General

Profile

Sample-vaadin-spring-hibernate » History » Revision 15

Revision 14 (Henning Blohm, 06.05.2018 17:35) → Revision 15/19 (Henning Blohm, 18.09.2018 21:45)

h1. A sample using Vaadin with Hibernate JPA and Spring on Z2 

 This sample is similar to [[Sample-spring-hibernate]] but differs (or rather extends) in that it show cases 
 the use of the "Vaadin":http://www.vaadin.com user interface toolkit in conjunction with Spring implemented annotation based 
 dependency injection over Z2 modularity.  

 As Spring is used throughout - in all modules - this is another practical application of [[How to Spring]]. 

 Moreover this sample illustrates modularization of Vaadin user interfaces with Z2 using extension points as described in [[Vaadin add-on]]. 

 This sample is stored in "z2-samples.vaadin-spring-hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate. 

 h2. Prerequisites 

 {{include(Java Version Requirements)}} 

 You need to run Java DB as network server on localhost. This is explained next. 

 The application will create a database "z2-samples" 

 {{include(How to run Java db)}} 


 h2. Run it 

 Like all samples, also this sample can be run as in [[How to run a sample]]. If you have the database, the fastest way to verify whether it runs is: 

 {{include(Install_sample_prefix)}} 

 Check out the sample 

 <pre><code class="ruby"> 
 mkdir install 
 cd install  
 git clone -b v2.6 v2.5 http://git.z2-environment.net/z2-base.core 
 git clone -b v2.5 http://git.z2-environment.net/z2-samples.vaadin-spring-hibernate 

 # on Linux / Mac OS: 
 cd z2-base.core/run/bin 
 ./gui.sh 

 # on Windows: 
 cd z2-base.core\run\bin 
 gui.bat 
 </code></pre> 

 {{include(Install_sample_postfix)}} 

 When running, go to http://localhost:8080/vaadin-spring-hibernate. You should see this: 

 !vaadin-spring-hibernate.png! 

 h2. Details 

 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. 

 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: 

 <pre><code class="xml"> 
         <!-- import external components --> 
         <bean id="thingyRepository" class="com.zfabrik.springframework.ComponentFactoryBean"> 
                 <property name="componentName" value="com.zfabrik.samples.vaadin-spring-hibernate.domain/repository" /> 
                 <property name="className" value="com.zfabrik.samples.vaadin_spring_hibernate.thingies.ThingyRepository" /> 
         </bean> 
 </code></pre> 

 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: 

 <pre><code class="xml"> 
 <!-- Vaadin --> 
 <servlet> 
   <servlet-name>VaadinServlet</servlet-name> 
   <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class> 
   <init-param> 
     <param-name>application</param-name> 
     <param-value>com.zfabrik.samples.impl.vaadin_spring_hibernate.ApplicationImpl</param-value> 
   </init-param> 
   <!-- this because Vaadin doesn't use the thread's context class loader by default --> 
   <init-param> 
     <param-name>ClassLoader</param-name> 
     <param-value>com.zfabrik.vaadin.ContextClassLoader</param-value> 
   </init-param>* 
 </servlet> 
 </code></pre> 

 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: 

 <pre><code class="java"> 
 @Configurable 
 public class ThingiesQuery implements Query { 
	 @Autowired 
	 private ThingyRepository repository; 
	 private Integer size; 
	 private boolean asc; 

 // ... 
 } 
 </code></pre> 

 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. 

 h3. Transaction Management 

 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: 

 <pre><code class="java"> 
 public class TransactionFilter implements Filter { 


         @Transactional 
         @Override 
         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
                 chain.doFilter(request, response); 
         } 

 } 
 </code></pre> 

 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]]). 

 h3. Extensibility 

 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. 

 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.  

 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 

 <pre><code class="bash"> 
 # 
 # A Vaadin UI extension  
 # 
 com.zfabrik.component.type=com.vaadin.ui.Component 

 # 
 # Coordinates: 
 # 
 com.zfabrik.vaadin.extension.point=com.zfabrik.samples.vaadin_spring_hibernate.actions 
 com.zfabrik.vaadin.extension.order=100 

 # 
 # The implementation class 
 # 
 component.className=com.zfabrik.samples.impl.vaadin_spring_hibernate.ext.SomethingAboutThingies 

 # 
 # make sure the app context is loaded first 
 # 
 com.zfabrik.component.dependencies=\ 
	  com.zfabrik.samples.vaadin-spring-hibernate.extension/applicationContext 
 </code></pre> 

 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: 

 <pre><code class="java"> 
 // find extensions and add them to the button row 
 for (Component c : ExtensionComponentsUtil.getComponentyByExtensionForApplication(application,"com.zfabrik.samples.vaadin_spring_hibernate.actions")) { 
     // and add them to this. 
     buttons.addComponent(c); 
 } 
 </code></pre>