Home > Blockchain >  Spring Boot implementation as Java9/Jigsaw Module
Spring Boot implementation as Java9/Jigsaw Module

Time:05-10

I have created a simple Spring Boot Interceptor class with a Config class to add that interceptor to the list of interceptors. This is inside a module of a project.

This is managed through Maven and the strufture is similar to:

.
├── interceptors
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   ├── com
│   │   │   │   │   └── mypackage
│   │   │   │   │      ├── config
│   │   │   │   │      │   ├── MyConfig.java
│   │   │   │   │      │   └── package-info.java
│   │   │   │   │      └── interceptors
│   │   │   │   │          ├── MyInterceptor.java
│   │   │   │   │          └── package-info.java
│   │   │   │   └── module-info.java
│   │   └── test
│   └── target
├── pom.xml
└── target

The project uses Maven and has some external Spring dependencies.

I have the below imports on my classes:

MyInterceptor:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

import org.slf4j.MDC;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

MyConfig:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.mypackage.interceptors.MyInterceptor;

The code works and gets compiled without issues both in the IDE and using Maven.

I have added a module-info.java file and imports aren't working anymore. Consequently, the project won't compile.

To fix the imports, I have added the below to the module-info.java file:

module interceptors {
    requires slf4j.api;
    requires spring.core;
    requires spring.webmvc;
    requires spring.context;
    requires org.apache.tomcat.embed.core;

    exports com.mypackage.config;
    exports com.mypackage.interceptors;
}

After doing this, the imports won't show up as red anymore, but when I do a maven clean install, I get these errors:

module not found: slf4j.api
module not found: spring.core
module not found: spring.webmvc
module not found: spring.context
module not found: org.apache.tomcat.embed.core

If I add them as maven dependencies, the behavior is still the same.

Can someone please explain to me in plain English what am I doing wrong and how to fix this? I am new to this project Jigsaw.

Use case: This module will later be added to another Spring Boot project and used as dependency there for the auto-configuration of beans with external configuration source. So, I am writing this interceptor class so that every Spring project will use it instead of copy-pasting the implementation. I assume I need this module-info.java to expose it to other packages. Please correct me if I'm wrong.

CodePudding user response:

This module will later be added to another Spring Boot project and used as dependency there using modules in a spring boot app does usually make no sense because you end up with a single jar file. module-info are typically needed/used for app which comprise of several jar files or jar's which are used as a library.

All spring/spring boot jars are not yet java modules (which means they don't have any module-info.class file...but they have an automatic-module-name entry in MANIFEST.MF) and tomcat-embed does not even contain an automatic-module-name.

Adding a maven dependency does not mean you can use a dependency as a java module...it's needed having at least an automatic-module-name etc

  • Related