简体   繁体   中英

Library jar to read .properties file from client CLASSPATH

I made a maven library jar which detect date formats based on configured formats mapping. Currently I hard-coded the formats mapping but I want to isolate the formats mapping into a .properties files as below:

  • Internal .properties file (has some default formats mapping).
  • External .properties file (dateformats.properties, a file that client may add it to his application CLASSPATH to extend the default formats mapping provided by the library).

Reading the internal .properties file from library CLASSPATH is easy by but how to let the library read the external .properties file from client CLASSPATH?

Number of options are available to you:

  • have the client specify the Properties instance that you'll use (so your API would recive the java.util.Properties typed as one of it's arguments)
  • search the classpath for other .properties files and prefer the client's one (get the java.class.path property and search the jar entries or be inventive with getResources method )
  • use the client's classloader (one method of doing this is examining the stack and determining he client's class and then access it's classloader, another would be to have the client pass it as a parameter)

Each method has its pluses and minuses and it depends upon your needs...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM