Users who have migrated Service Registry to Hibernate Core 4.0 (non-JPA) may have noticed that the well-known method for constructing SessionFactory is no longer recommended: Configuration configuration = new Configuration (); sessionFactory sf = configuration. buildSessionFactory (); the recommended method in Hibernate ORM 4 is org. hibernate. cfg. configuration # buildSessionFactory (ServiceRegistry serviceRegistry), you must first construct a ServiceRegistry object. So, is Shenma ServiceRegistry? As the name suggests, ServiceRegistry is the Service registry, which provides a unified loading, initialization, storage, and retrieval mechanism for the Service. first, let's take a look at the overall class diagram: The entire ServiceRegistry structure is similar to the Classloader proxy structure we all know, except that the Classloader first proxies to the parent node, the parent node cannot be found and then the search policy is different from the Classloader Of the current level. ServiceRegistry searches for the parent node first and then finds the parent node. We can see that ServiceRegistry in hibernate is actually composed of three layers (range): BootstrapServiceRegistry mainly provides Classloading and Service loading services for global use. serviceRegistry standard ServiceRegistry also serves global services. When initializing services in this Registry, you may need to use services in BootstrapServiceRegistry. sessionFactoryServiceRegistry is associated with SessionFactory (one-to-one relationship). Services need to access SessionFactory during initialization. therefore, BootstrapServiceRegistry mainly includes basic services to be used globally, while ServiceRegistry mainly includes Services related to Hibernate on rviceRegistry, such as configuration file parsing, JDBC, and JNDI. sessionFactoryServiceRegistry is related to sessionfactory, including 2LC region factory and event listener. the hierarchical relationship is completely transparent to the service registry client. You can directly call getService to obtain the service you need without worrying about the level at which you are located, like classloader. so what is Service? Simply put, a Service is a function that consists of an interface (Service Role) and an implementation class. the Service is not a new concept in Hibernate. In previous versions, we already have these things. This time, we put the Service providers (classes ), the Service is extracted into one Service, so that you can access, load, and initialize the Service in a unified way. lists all the built-in Services in Hibernate. For more information, see Building ServiceRegistry, currently, the standard way to construct a SessionFactory is to pass a ServiceRegistry instance to the Configuration # buildSessionFactory method. Therefore, we need to first create a ServiceRegistry instance. the simplest method is StandardServiceRegistryBuilder se. RviceRegistryBuilder = new StandardServiceRegistryBuilder (); ServiceRegistry serviceRegistry = serviceRegistryBuilder. build (); SessionFactory sf = configuration. buildSessionFactory (serviceRegistry); let's continue to analyze what happened here. as described above, ServiceRegistry is a proxy structure. In fact, you need to create BootstrapServiceRegistry, ServiceRegistry, and SessionFactoryServiceRegistry respectively. bootstrapServiceRegistryorg. hibernate. service. serviceRegist RyBuilder has two constructor functions. One is to accept a BootstrapServiceRegistry instance as a parameter, and the other is not a parameter. You can construct an org internally. hibernate. boot. registry. internal. bootstrapServiceRegistryImpl instance. as mentioned above, we actually built a BootstrapServiceRegistry first. hibernate also provides org. hibernate. boot. registry. bootstrapServiceRegistryBuilder. through this builder class, we can call the following method to expand some basic services: with (Integrator integrator) with (ClassLoader classLoader) withStrategySelector Www.2cto.com (for more information about the extension here, please wait for the next article). Now we have BootstrapServiceRegistryBuilder and ServiceRegistryBuilder. Naturally, I think there is also a SessionFactoryServiceRegistryBuilder? Sorry, no! Yes, but we provide org. hibernate. service. spi. sessionFactoryServiceRegistryFactory is a standard service located in ServiceRegistry. this is mainly because all of its work is to create a meaningless org. hibernate. service. internal. sessionFactoryServiceRegistryImpl instance is not extensible, so a builder is not needed. so, if you have to replace the default implementation, or, further, for example, you want to replace the standard implementation of a Service, do you want to add your own service to service registry? Let's take a closer look at the Service. First, the service provided in BootstrapServiceRegistry is fixed and cannot be increased or decreased unless you provide your own org. hibernate. boot. registry. internal. the BootstrapServiceRegistryImpl implementation class is not recommended. from org. hibernate. boot. registry. internal. in the BootstrapServiceRegistryImpl # locateServiceBinding method, we can see the standard service provided by BootStrapServiceRegistry. public <R extends Service> ServiceBinding <R> locateServiceBinding (Class <R> serviceRole) {if (ClassLoaderService. Class. equals (serviceRole) {return (ServiceBinding <R>) classLoaderServiceBinding;} else if (StrategySelector. class. equals (serviceRole) {return (ServiceBinding <R>) strategySelectorBinding;} else if (IntegratorService. class. equals (serviceRole) {return (ServiceBinding <R>) integratorServiceBinding;} return null;} The recommended extension location is ServiceRegistry. Here, you can add your custom service, replacement of standard service implementation and so on. se The standard service provided in rviceRegistry is defined in org. hibernate. service. among StandardServiceInitiators: public class StandardServiceInitiators {public static List <StandardServiceInitiator> LIST = buildStandardServiceInitiatorList (); private static List <StandardServiceInitiator> buildStandardServiceInitiatorList () {final List <StandardServiceInitiator> serviceInitiators = new ArrayList <StandardServiceInitiator> (); servic EInitiators. add (ConfigurationServiceInitiator. INSTANCE); serviceInitiators. add (ImportSqlCommandExtractorInitiator. INSTANCE); serviceInitiators. add (JndiServiceInitiator. INSTANCE); serviceInitiators. add (JmxServiceInitiator. INSTANCE); serviceInitiators. add (PersisterClassResolverInitiator. INSTANCE); serviceInitiators. add (PersisterFactoryInitiator. INSTANCE); serviceInitiators. add (Connectio NProviderInitiator. INSTANCE); serviceInitiators. add (MultiTenantConnectionProviderInitiator. INSTANCE); serviceInitiators. add (DialectResolverInitiator. INSTANCE); serviceInitiators. add (DialectFactoryInitiator. INSTANCE); serviceInitiators. add (BatchBuilderInitiator. INSTANCE); serviceInitiators. add (JdbcEnvironmentInitiator. INSTANCE); serviceInitiators. add (JdbcServicesInitiator. INSTANCE); se RviceInitiators. add (RefCursorSupportInitiator. INSTANCE); serviceInitiators. add (SchemaManagementToolInitiator. INSTANCE); serviceInitiators. add (MutableIdentifierGeneratorFactoryInitiator. INSTANCE); serviceInitiators. add (JtaPlatformInitiator. INSTANCE); serviceInitiators. add (TransactionFactoryInitiator. INSTANCE); serviceInitiators. add (SessionFactoryServiceRegistryFactoryInitiator. INSTANCE ); Return Collections. unmodifiableList (serviceInitiators) ;}} we can see that every standard service is encapsulated into StandardServiceInitiator. What is ServiceInitiator? ServiceInitiatororg. hibernate. service. spi. serviceInitiator defines a Service loader. There is only one method in this interface/*** Obtains the service role initiated by this initiator. shoshould be unique within a registry ** @ return The service role. */public Class <R> getServiceInitiated (); While org. hibernate. service. standardServiceInitiator inherits org. hibernate. service. spi. serviceInitiator provides another method:/*** Initiates the managed service. ** @ param configurationValues The configuration values in effect * @ param registry The service registry. can be used to locate services needed to fulfill initiation. ** @ return The initiated service. */public R initiateService (Map configurationValues, ServiceRegistryImplementor registry); As shown in, ServiceInitiator defines a Service interface and how to initialize this service. therefore, after ServiceInitiator is available, you can call org. hibernate. boot. registry. the StandardServiceRegistryBuilder method is added to the Registry: addInitiator (StandardServiceInitiator initiator) addService (final Class serviceRole, final Service service) hibernate initializes the built-in services before initializing these services, if you want to replace the standard service, you only need to call one of the above two methods as is. hibernate also provides some interfaces for Service creators to use (for more information about how to use them, see javadoc. We will not repeat them here): org. hibernate. service. spi. configurableorg. hibernate. service. spi. manageableorg. hibernate. service. spi. serviceRegistryAwareServiceorg. hibernate. service. spi. injectServiceorg. hibernate. service. spi. startableorg. hibernate. service. spi. stoppableorg. hibernate. service. spi. wrapped