Example example_spring-hibernate
Part of the full Java 9 Jigsaw modules example suite.
|
Authors
Originally written by Martin Lehmann, Kristine Schaal and RĂ¼diger Grammes (cf. original repository). Migrated for Java Modules support documentation of Apache MavenTM in the course of the Maven Support & Care program by Gerd Aschemann (and other team members) as forked repository. Please add discussions, requirements, bugfixes, etc. to the fork instead of the original. |
What is this example about?
-
Running a Spring Boot application with Java9.
-
Using modules
Modules in this example
-
currently only module mod.app, containing the main class (the SpringBootApplication to be startet by Spring Boot)
Module Dependency Graph, created with DepVis
TODO
Example shows …
A Java 9 application, using Spring and Hibernate, based on Spring Boot 2.x. There are clean-, compile- and run scripts to build and start tests, resp. to run the application.
Currently work in progress. Below see the steps we did so far to get this state:
-
Spring Boot application, with one module (
mod.app) containing only the (empty) SpringBootApplication. -
Compiles and tests ok with Java 11+
-
Runs with Java 11+, via the Maven
spring-boot-plugin, or as fat jar via CLI.
Step A: Preparing the project, Bootstrap with Java 8
-
Configured and downloaded our application with the Spring initializr using Spring Boot 2.0.0 M5, to be run with maven. We included
-
Security for authentication and authorization
-
Jersey for REST services
-
JPA persistence
-
H2 database
-
Now the application starts with Java 8.
Step B: Prepare for Java 9+ - without modules
-
To compile and test, the Maven surefire plugin version 2.20.1 is needed.
java.xml.bindhas to be added as module for test:--add-modules java.xml.bind -
We use the
spring-boot:rungoal to run the application (seerun.sh). The argument--add-modules java.xml.bindhas to be configured here, too. -
We introduced the module
mod.app. It contains theSpringBootApplication -
To get this compiled, we had to drop
jersey(commented out inpom.xml. Otherwise we ran into problems withasm(version conflict with the asm version the compiler needs). -
The compiler needs a dependency to
asm-6.0. So we had to exclude theasmversion fromplexus-java(which wantsasm-6.0-BETA) and set it explicitely for the compiler.
The application now compiles, tests and runs with Java 9.
Step C: Introducing a Java Module
-
We converted
mod.app`into a module by adding a `module-info. -
We then needed to solve more bytecode enhancement issues. Therefore we declared the main class explicitely in the
spring-boot:run-plugin. This avoids letting Spring Boot search a main class itself, see https://github.com/spring-projects/spring-boot/issues/10647 -
Resolving a
javassistproblem by using the newest version 3.22.0-GA, see https://stackoverflow.com/questions/46132019/java-io-ioexception-invalid-constant-type-19-at-5 -
Open the module (Adding
opensdirective to module-info.java).
|
The SpringBoot Application (module) becomes a top level module, i.e., it is directly loaded as the starting point of the application. It is hardly ever consumed as a module. Therefore, opening the module will do no harm. TODO: Extend the application with further (sub-) modules. |
The application now compiles, tests and runs with Java 11+ and with a module-info.java.
Note that both tests and launch are still done on the classpath.
TODOs, LOP, Backlog, Ideas, …
No software is ready, ever ;-) So here are some ideas left (any other feedback very welcome!):
-
Introduce more modules
-
Run application on the module path
-
Introduce own entities for JPA persistence
-
Introduce own Spring beans
Output and Testing
This example does not use golden master testing because Spring Boot output includes timestamps, port numbers, absolute file paths, and environment-specific information that varies between runs. See the golden master testing section in the main README for more information.