How does Java find the specification implementation?

I do not understand how Java knows the implementation of any specification.

For example, I have a Spring application with JSF if I put a Mojarra jug in a classpath that works in the application, but I did nothing except add it to the classpath, without configuration

If I take out a can of Mojarra, then my application does not work.

Even how can it differ between Mojarra or MyFaces without any changes?

I just want to know how Java finds implementations, and not why it doesn't work if I take out the jars;)

+4
source share
3 answers

I saw the mojarra banner (JSF Reference Implementation) and I found that it uses the service loader API as it hosts the javax.servlet.ServletContainerInitializer, javax.enterprise.inject.spi.Extension and com.sun. faces.spi.injectionprovider inside the / META -INF / services folder in javax.faces-2.2.0.jar.

This is how Java distinguishes between an implementation and another (mojarra or myfaces).

0
source

Typically, Java specification classes are divided into two parts:

  • The part of the API (jar) that contains mainly the interfaces usually provided by development guys.
  • The implementation part (jar) that contains the implementations of these interfaces.

An example of the above is JDBC: javax.sql.* Classes are provided by sun/oracle , but implementation is done in the JDBC drivers provided by each db provider. In this case, some configuration is needed to "map" the API to its implementation, because you only use the API classes.

Your JSF example is slightly different in that both Mojarra and MyFaces contain the API classes from the javax.faces.* Package, basically there are different classes with the same names. Therefore, there is no need to configure, classes have the same name in both libraries and are loaded by the class loader whenever necessary from Mojarra or MyFaces cans, depending on what is in the class path.

+2
source

I found an implementation of MyFaces and Mojarra for JSF. I realized that both have the same structure as anything that complies with the JSF specification. In other words, the jar implementation is defined in the classpath classpath, the handler will process it.

This is all about the Java Load Loader mechanism of the JVM. You can find a detailed discussion on Java Class Loaders

Here is a picture of the structure of the JSF implementation package.

enter image description here

0
source

All Articles