The principle of implementation of JSR107 API and SPI frameworks in the Java class library

JSR107 is an abbreviation of Java specification requests. It defines the cache framework specification of Java and aims to provide a universal cache interface for Java applications.API (application interface) and SPI (service provider interface) are two important parts of the JSR107 specification, and the cache framework is implemented in the Java class library. API is a set of interfaces defined in the JSR107 specification, which is used to describe the basic function of cache operation.It defines the commonly used operation methods of cache, such as Put, Get, Remove, etc.Developers can achieve their cache classes and integrate them into applications in accordance with the API specifications.Below is a simple example code that shows how to use the API to operate the cache: import javax.cache.Cache; import javax.cache.CacheManager; import javax.cache.Caching; import javax.cache.configuration.MutableConfiguration; public class CacheExample { public static void main(String[] args) { // Create a cache manager CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(); // Create a cache configuration MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setStorebyValue (false); // The cache value does not require deep copy // Create a cache Cache<String, String> cache = cacheManager.createCache("myCache", configuration); // Add value to cache cache.put("key1", "value1"); // Get the value from the cache String value = cache.get("key1"); System.out.println(value); // Move the value from the cache cache.remove("key1"); // Turn off the cache manager cacheManager.close(); } } SPI is another important concept defined in the JSR107 specification, which is used to expand the cache framework function.SPI provides a mechanism that allows developers to add new functional modules to the application without modifying the framework source code.Specifically, SPI allows developers to define a set of interfaces and implementation classes, and then load the implementation information into the application through configuration files. In the JSR107 specification, the cache provider must implement the `javax.cache.spi.cachingprovider` interface and provide the implementation class of the interface.This implementation class will be loaded when the application initializes and is registered as the default cache provider.The following is a simple example code that shows how to use the SPI extension cache framework: import javax.cache.Cache; import javax.cache.CacheManager; import javax.cache.Caching; import javax.cache.spi.CachingProvider; import javax.cache.configuration.MutableConfiguration; public class CacheExample { public static void main(String[] args) throws Exception { // Load the cache provider CachingProvider cachingProvider = Caching.getCachingProvider(); // Create a cache manager CacheManager cacheManager = cachingProvider.getCacheManager(); // Create a cache configuration MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setStorebyValue (false); // The cache value does not require deep copy // Create a cache Cache<String, String> cache = cacheManager.createCache("myCache", configuration); // Add value to cache cache.put("key1", "value1"); // Get the value from the cache String value = cache.get("key1"); System.out.println(value); // Move the value from the cache cache.remove("key1"); // Turn off the cache manager cacheManager.close(); // Close the cache provider cachingProvider.close(); } } In summary, the JSR107 cache framework realizes the definition and expansion of the cache operation through the cooperation of the API and SPI.API defines the basic operation method of cache, and developers can achieve their cache class according to these standard interfaces.SPI provides a mechanism of expansion framework function, which can flexibly expand the cache frame function by adding an implementation method.