Migrate the Spring Boot application to the Java 9-module __java

Source: Internet
Author: User
Configuring module Meta Data

The module information in Java 9 is implemented through the Module-info.java file. The first step is to create a file in the root directory of the source directory with the module name:

Module Org.springframework.samples.petclinic {
}

The rest of the journey may be heaven or hell. But I'm lucky. Benefiting from IntelliJ idea, the IDE wizard accurately says that it cannot read the class and allows you to put it into the module file. Finally, it looks like this:

module org.springframework.samples.petclinic {    requires java.xml.bind;
    requires javax.transaction.api;
    requires validation.api;
    requires hibernate.jpa;
    requires hibernate.validator;
    requires spring.beans;
    requires spring.core;
    requires spring.context;
    requires spring.tx;
    requires spring.web;
    requires spring.webmvc;
    requires spring.data.commons;
    requires spring.data.jpa;
    requires spring.boot;
    requires spring.boot.autoconfigure;
    requires cache.api; }

Note that the module configuration in Maven-compiler-plugin and Maven-surefire-plugin can be deleted.

do not use the IDE for configuration

If you happen to have no ideal IDE environment, the configuration process is as follows:

Run MVN clean test

Parse the error log to get the missing package name

Navigate to the package mentioned above

If this is a module Jar, add the module name to the list of dependent module names

Otherwise, guess the automatic module name (Automatic module name) is intended to be used in Java 9 projects for packages that are not modularized (that is, without module-info). Their names are usually to remove the version number of the jar package file name, and then add to the list of dependent module names

For example:

[ERROR] ~/spring-petclinic/src/main/java/org/springframework/samples/petclinic/system/cacheconfig.java:[21,16]
Package Javax.cache is not visible
[ERROR] (Package Javax.cache is declared in the unnamed module, but module Javax.cache does not read it)

Javax.cache is located in Cache-api-1.0.0.jar, because this is not a module, so there is no Module-info file in the JAR package. But it is known that its automatic module name is CACHE.API. Writes it to a module dependency. Repeat the above steps. ASM Failure

Starting with the first part of this article, I have realised that Spring Boot 1.5 will not be compatible with Java 9. Here we go.

Upgrading the Spring Boot to 2.0.0.M5 requires some changes to the module dependencies:

Hibernate.validator into Org.hibernate.validator

Validation.api into Java.validation

When you think it can work:

caused by:java.lang.RuntimeException at
	Org.springframework.asm.ClassVisitor.visitModule (Classvisitor.java : 148)

The question has been put on record. At this point, the explicit declaration of the main class solves the problem.

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-maven-plugin</artifactid>
<configuration>
<jvmarguments>--add-modules ja Va.xml.bind</jvmarguments>
<mainClass>    org.springframework.samples.petclinic.petclinicapplication</mainclass>
</configuration>
...
</plugin>
Javassist Failure

Now the app can be tested with the following command: MVN clean spring-boot:run. Unfortunately, a new anomaly is blocking our way forward:

2017-10-16 17:20:22.552  info 45661 --- [            main] utoconfigurationreportlogginginitializer : Error starting  ApplicationContext. To display the auto-configuration report re-run 
your application with  ' Debug '  enabled. 2017-10-16 17:20:22.561 error 45661 --- [            main] o.s.boot.SpringApplication        

       :  application startup failed Org.springframework.beans.factory.BeanCreationException:   error creating bean with  name  ' entitymanagerfactory '  defined in class path resource     [org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaautoconfiguration.class]: &nbsP;   invocation of init method failed; nested exception is  org.hibernate.boot.archive.spi.archiveexception:      could not build  classfile

Fast Search for an incompatibility problem with Java 9 and javassit. Javassist is the culprit here. Its dependencies require Spring Data JPA, passed through Hibernate. To resolve it, you need to remove the dependencies and add the latest version:

<dependency>     <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-data-jpa</artifactId>     < Exclusions>         <exclusion>              <artifactId>javassist</artifactId>              <groupId>org.javassist</groupId>          </exclusion>     </exclusions> </ Dependency> <dependency>     <groupId>org.javassist</groupId>      <artifactId>javassist</artifactId>     <version>3.22.0- Ga</version>     <scope>runtime</scope> </dependency>

Fortunately, this version is compatible--at least for our use. it's running.

We did it! If you come to this step, you should clap your shoulders, have a beer, or whatever you think you deserve.

The icing on the cake is that the dependencies of the development tools can be added back. Conclusions

Whether you like it or not, migrating to Java 9 requires the use of Jigsaw. At the very least, this means a painful journey, and the process of searching for the next patch when you encounter errors, and removing important steps in the build process. It is interesting for library/framework developers to add additional access control layers to their internal APIs, which is less for applications. At this stage, it is not worth migrating to Java 9.

I hope to carry out this experiment in a few months ' time and then pay attention to the improvement of the situation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.