Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice For Referencing an External Module In a Java Project

I have a Java project that expects external modules to be registered with it. These modules:

  • Implement a particular interface in the main project
  • Are packaged into a uni-jar (along with any dependencies)
  • Contain some human-readable meta-information (like the module name).

My main project needs to be able to load at runtime (e.g. using its own classloader) any of these external modules. My question is: what's the best way of registering these modules with the main project (I'd prefer to keep this vanilla Java, and not use any third-party frameworks/libraries for this isolated issue)?

My current solution is to keep a single .properties file in the main project with key=name, value=class |delimiter| human-readable-name (or coordinate two .properties files in order to avoid the delimiter parsing). At runtime, the main project loads in the .properties file and uses any entries it finds to drive the classloader.

This feels hokey to me. Is there a better way to this?

like image 786
G__ Avatar asked Oct 15 '22 06:10

G__


1 Answers

The standard approach in Java is to define a Service Provider.

like image 132
ZZ Coder Avatar answered Oct 20 '22 17:10

ZZ Coder