The OP asks a general question about how implementation is introduced in some different cases.
entrance
As many answers SLF4J
, SLF4J
gives an interface, and log4j-slf4j
gives an implementation.
When you use the following statement:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; ... private static final Logger LOG = LoggerFactory.getLogger(FooBarClass.class); ... LOG.debug("Foobar");
This is what happens:
We are trying to get Logger
from the getLogger
method declared in the LoggerFactory
class :
public static ILoggerFactory getILoggerFactory() { if (INITIALIZATION_STATE == UNINITIALIZED) { synchronized (LoggerFactory.class) { if (INITIALIZATION_STATE == UNINITIALIZED) { INITIALIZATION_STATE = ONGOING_INITIALIZATION; performInitialization(); } } } switch (INITIALIZATION_STATE) { case SUCCESSFUL_INITIALIZATION: return StaticLoggerBinder.getSingleton().getLoggerFactory(); } ... }
So, the magic happens in this statement:
return StaticLoggerBinder.getSingleton().getLoggerFactory();
Since the classpath knows that you implemented why, the StaticLoggerBinder
implementation is provided by log4j
. As we can see, log4j
provides its own implementation:
private final ILoggerFactory loggerFactory; ... private StaticLoggerBinder() { loggerFactory = new Log4jLoggerFactory(); }
What is it!
Constancy
For the JPA / Hibernate part, you should include hibernate-jpa-api
and hibernate-*
(core, entitymanager, etc.).
Suppose you want to create an EntityManagerFactory
:
import javax.persitence.EntityManagerFactory import javax.persitence.Persistence; ... private static EntityManagerFactory EMF = Peristence.createEntityManagerFactory("foobar", null);
As for List
and ArrayList
, your class path will be equipped with an interface and implementation thanks to the imported JAR servers.
EntityManagerFactory
comes from hibernate-jpa-api
, where we have a Persistence
class . We can notice that the createEntityManagerFactory
method first lists all the providers , and createEntityManagerFactory
run for each of them. Here comes hibernate
. It provides a HibernatePersistenceProvider
that implements the PersistenceProvider
class .
This is how hibernate
is introduced.