rvillablanca
rvillablanca

Reputation: 1646

How does Java find a specification implementation?

I don't understand how java knows an implementation of any spec..

For example, I have a Spring App with JSF, if I place the Mojarra jar on classpath the app works, but I have not done nothing but that add it to classpath, all without configuration

If I take out the Mojarra jar, then my app doesn't works.

Even, How it can differenciate between Mojarra or MyFaces without any change?

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

Upvotes: 4

Views: 288

Answers (3)

rvillablanca
rvillablanca

Reputation: 1646

I've seen the mojarra jar (JSF Reference Implementation) and I found that it uses The Service Loader API, since it places the javax.servlet.ServletContainerInitializer, javax.enterprise.inject.spi.Extension and com.sun.faces.spi.injectionprovider inside the /META-INF/services folder in the javax.faces-2.2.0.jar.

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

Upvotes: 0

Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

I have found MyFaces and Mojarra Implementation for JSF. I have realized that both has same pachage structure so it means that all are compliance with JSF spec. In other word which implementation jar is given into classpath classpath loader will handle it.

It is all about Java Class Loader mechanism of JVM. You can find detailed discussion at Java Class Loaders

Here is the picture of both JSF implemantation package structure.

enter image description here

Upvotes: 0

dcernahoschi
dcernahoschi

Reputation: 15230

Usually the Java spec classes are divided in two parts:

  1. An API part(jar) that contains mainly interfaces usually provided by the spec guys.
  2. An implementation part(jar) that contain the implementations of these interfaces.

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

Your example with JSF is a little different in that both Mojarra and MyFaces contains the API classes from javax.faces.* package, basically there are different classes with the same names. So there is no need for configuration, the classes have the same name in both libraries and are loaded by the class loader whenever needed from the Mojarra or MyFaces jars whichever is on the classpath.

Upvotes: 2

Related Questions