隐藏服务实现的最佳做法

隐藏服务实现的最佳做法

本文介绍了隐藏服务实现的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从API用户隐藏一个服务(接口)的实现(具体类)。该实现由使用Java API ServiceLoader 机制的工厂提供给用户。此Loader需要实现类具有公共可见性。只要实现被隐藏在不同的JAR(除了API JAR之外),用户并不直接依赖于这一点,这是可以的。



然而,为了方便分发具有默认实现的JAR的内容被打包到API JAR中。因此,用户有效地依赖于这种预先插入的JAR,其中默认实现类可用于公共可见性。没有什么可以让人们不直接实例化。我不想要这样做。



我的坏主意:




  • 实现一个自己的ServiceLoader版本,它允许加载包私有实现(btw,为什么Java API ServiceLoader不允许这个?)

  • 运送单独的API和实现JAR



你认为正确的方式是什么?任何妥协?



使用OSGi或其他重型机械是无关紧要的。

解决方案>

令人失望的可能是:




  • 制作一个单独的jar,并与其他第三方的jar一起使用。

  • 让安装程序/部署过程处理好所有的包装。

  • 不要在编译时使这个jar可用; maven:< scope>运行时< / scope>



另一种方式是在javadoc中有 @Deprecated ,作为注释,如何使用java服务API使用相应的类。


I want to hide the implementation (concrete class) of a service (interface) from the API user. The implementation is provided to the user by a factory which uses the Java API ServiceLoader mechanism. This Loader requires the implementation class to have public visibility. This is OK as long as the implementation is hidden in a different JAR (apart from the API JAR) the user is not directly depending on.

However, for easy distribution the content of the JAR with the default implementation is packed into the API JAR. So effectively the user depends on this prepacked JAR where the default implementation class is available with public visibility. Nothing keeps people from directly instantiating the implementation. I don't want that to be possible.

My bad ideas:

  • implement an own version of ServiceLoader which allows to load package private implementations (btw, why does the Java API ServiceLoader doesn't allow this?)
  • ship separate API and implementation JARs

What do you think is the right way? Any compromise?

Using OSGi or other heavy machinery is out of question.

解决方案

Disappointing maybe but:

  • Make a separate jar and put that with the other third party jars.
  • Let the installer/the deployment process handle nice packaging of all.
  • Do not make this jar available during compile time; maven: <scope>runtime</scope>.

The only other way would be the have @Deprecated in the javadoc, with as comment how to use the corresponding class using the java service API.

这篇关于隐藏服务实现的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:13