简体   繁体   English

库jar从客户端CLASSPATH读取.properties文件

[英]Library jar to read .properties file from client CLASSPATH

I made a maven library jar which detect date formats based on configured formats mapping. 我制作了一个maven库jar,它基于配置的格式映射来检测日期格式。 Currently I hard-coded the formats mapping but I want to isolate the formats mapping into a .properties files as below: 当前,我对格式映射进行了硬编码,但我想将格式映射隔离为.properties文件,如下所示:

  • Internal .properties file (has some default formats mapping). 内部.properties文件(具有一些默认格式映射)。
  • 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). 外部.properties文件(dateformats.properties,客户端可以将其添加到其应用程序CLASSPATH中的文件,以扩展库提供的默认格式映射)。

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? 从库CLASSPATH读取内部.properties文件很容易,但是如何让该库从客户端CLASSPATH读取外部.properties文件呢?

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) 让客户端指定您将使用的Properties实例(这样,您的API会接收作为其参数之一键入的java.util.Properties)
  • 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 ) 在类路径中搜索其他.properties文件,并优先选择客户端的.properties文件(获取java.class.path属性并搜索jar条目,或者使用getResources方法进行创新
  • 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... 每种方法都有其优缺点,取决于您的需要...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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