Defining a public library interface in Java

What are the options for defining a common interface for a library in Java.

For example, I often find that things are publicly available because they need a different package in the library (although there is still a common base package, for example com.stackoverflow.mylib.), Therefore they cannot have a level of access to packages and usually people don’t want bulk packages (it also seems that people using Spring insist on having separate controller / service / model / impl / etc packages, as a result of which one “function” is forced to cover many packages when they say that this service can be completely internal implementation detail is not for external use lzovaniya ...).

Thus, the ideal goal is to make the Jar that I have provided to third parties so that it is clear that these things cannot be used, ideally without them at all (not present in the API bank), so it is impossible for them to use and compile with these internal objects / methods.

Even more ideal for objects that are only supposed to be obtained from a kind of factory (for example, provided by Spring Bean), is a way to prevent direct instantiation from your code or custom bean (which some future, not yet represented property may uninitialized after the update).

Two official ways that I know at present:

  • Some projects that I have been working on have an api package (for example, com.stackoverflow.mylib.api), and the rule is only that the contents of this package can be accessed directly by external users.
  • In some other projects that I worked on, there were some user attributes, for example. @PublicSDK for designating objects and methods for use by the public (and I think some additional materials to ensure that only those tagged as such are in the public javadoc and api jar).
+4
source share
1 answer

The first thing to ask yourself: do you really need to hide implementation details?

, , , , , .

, API , , , ; , , , , .

API , , , imho, ( ).

, , API, , , : , API, - API.

Maven Gradle , API, jar ( API) ().

API- Java, JSON API, Glassfish.

+1

All Articles