java modules: provides... uses don't work in webapp

Viewed 59

I'm working on a project based on Java modules. I've a parent project (pom project) that contains other modules, a war module and some jar modules. I want to that a module is a provider, so I've defined a provider class in a module that will be used in an nother module. During the application startup of the webapplication, defined as following:

@WebListener
public class ServletContext implements ServletContextListener ..

I want to get a list of all the services that are provided from a module, so I have used the command:

ServiceLoader<MyClass> services = ServiceLoader.load(MyClass.class);

Unfortunately this instruction doesn't retrieve any service.

So, I've made a new project and tried it to understand if I made some mistakes. In the new project, the ServiceLoader.load, works properly. In this case I'm not using a servlet to test it but I ran it through a Main class. I'm taken these classeds and put in my original project and tried to run them using a Main class, and again the ServiceLoader.load works properly.

It seems that the ServiceLoader.load doesn't work when I invoke it from the web class, ServletContext.

I'm using Java 11, Tomcat 9.0.40.

Is there someone that has already faced this issue?

1 Answers

you can have a file in classpath, just like

src
|-- main
    |-- resources
        |-- META-INF
            |-- services
                |-- org.apache.logging.log4j.util.PropertySource       

replace org.apache.logging.log4j.util.PropertySource to your own interface name the content text of the file is class name which implements interface, just like

org.apache.logging.log4j.util.EnvironmentPropertySource
Related