Making JPMS ServiceLoader non-recursive or avoid duplicate services

I am trying to load modules dynamically from a folder.

Suppose I have a “Module A” that has its own set of dependencies and “Module B” that has its own set of dependencies + it also relies on some functionality from “Module A”. Each set of dependencies must be independent, to avoid the conflicts if multiple modules rely have the same dependency but of different versions. Then, the layer tree will look like this (each box is a separate layer):

ModuleLayers Tree
(I do not have enough reputation to post images)

However, with that Module Layers configuration if I try to run a ServiceLoader:

ServiceLoader.load(allModulesLayer, MyService.class);

I get the following array: [ moduleA.ServiceImpl@..., moduleB.ServiceImpl@..., moduleA.ServiceImpl@... ], so it seems like the ServiceLoader simply walks the layers from the given layer until it reaches the boot layer, resulting in two paths, where it encounters moduleA (allModules -> moduleA -> moduleADeps -> boot and allModules -> moduleB -> moduleA -> moduleADeps -> boot).

The same is true if I do not create the allModulesLayer, and just directly call ServiceLoader on each module layer:

ServiceLoader.load(layerA, MyService.class); // -> moduleA.ServiceImpl
ServiceLoader.load(layerB, MyService.class); // -> moduleB.ServiceImpl, moduleA.ServiceImpl

How can I make it so that ServiceLoader only loads services non-recursively, only from the given layer, that way I can just run ServiceLoader on each moduleX layer separately (like in the latter example), and join the Lists. Or somehow make it so that ServiceLoader does not load duplicate classes (and use the common allModulesLayer like in the former example).

(Edit: layerB.modules() only shows moduleB, so the layer does not contain moduleA, yet ServiceLoader somehow discovers it)

Assuming that the finder is a ModuleFinder of a folder, where all the module extensions are, to load module A, I do:

// Dependencies for "moduleA" are located at "./moduleA/dependencies":
ModuleFinder finderADeps = ModuleFinder.of(Paths.get("moduleA", "dependencies"));
// Get all the dependencies from the folder:
List<String> allADeps = finderADeps.findAll().stream().map(ModuleReference::descriptor).map(ModuleDescriptor::name).toList();
// Resolve all the dependencies using the previously defined finder:
Configuration configADeps = ModuleFinder.boot().configuration().resolve(finderADeps, ModuleFinder.of(), allADeps);
// Load the modules into the ["Module A" Dependencies Layer]:
ModuleLayer layerADeps = ModuleFinder.boot().defineModulesWithOneLoader(configADeps, ClassLoader.getSystemClassLoader());

// Define the configuration for ["Module A" Layer] that is a child of the Dependencies Layer Configuration:
Configuration configA = Configuration.resolveAndBind(finder, List.of(configADeps), ModuleFinder.of(), Set.of("moduleA"));
// Load the modules into the ["Module A" Layer] that is a child of the Dependencies Layer:
ModuleLayer layerA = ModuleLayer.defineModulesWithOneLoader(configA, List.of(layerADeps), ClassLoader.getSystemClassLoader()).layer();

Loading module B is almost identical, but when we form the “Module B” Layer:

// Add "configA" as a parent too:
Configuration configB = Configuration.resolveAndBind(finder, List.of(configA, configBDeps), ModuleFinder.of(), Set.of("moduleB"));
// Add "layerA" as a parent too:
ModuleLayer layerB = ModuleLayer.defineModulesWithOneLoader(configB, List.of(layerA, layerBDeps), ClassLoader.getSystemClassLoader()).layer();

module-info are as follows:

moduleA:

import ...;

module moduleA {
    requires core.api;
    exports com.module_a;
    provides MyService with ServiceImpl;
    ... other dependencies
}

moduleB:

import ...;

module moduleB {
    requires core.api;
    exports com.module_b;
    requires static moduleA;
    provides MyService with ServiceImpl;
    ... other dependencies
}

New contributor

Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật