org.springframework.core.type.classreading

## Class CachingMetadataReaderFactory

- java.lang.Object
- - org.springframework.core.type.classreading.SimpleMetadataReaderFactory
  - - org.springframework.core.type.classreading.CachingMetadataReaderFactory

- All Implemented Interfaces:
    MetadataReaderFactory

      public class CachingMetadataReaderFactory
      extends SimpleMetadataReaderFactory

  Caching implementation of the `MetadataReaderFactory` interface, caching a `MetadataReader` instance per Spring `Resource` handle (i.e. per ".class" file).
  Since:
    2.5
  Author:
    Juergen Hoeller, Costin Leau

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_CACHE_LIMIT` Default maximum number of entries for a local MetadataReader cache: 256.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CachingMetadataReaderFactory()` Create a new CachingMetadataReaderFactory for the default class loader, using a local resource cache.
    `CachingMetadataReaderFactory(ClassLoader classLoader)` Create a new CachingMetadataReaderFactory for the given `ClassLoader`, using a local resource cache.
    `CachingMetadataReaderFactory(ResourceLoader resourceLoader)` Create a new CachingMetadataReaderFactory for the given `ResourceLoader`, using a shared resource cache if supported or a local resource cache otherwise.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clearCache()` Clear the local MetadataReader cache, if any, removing all cached class metadata.
    `int` | `getCacheLimit()` Return the maximum number of entries for the MetadataReader cache.
    `MetadataReader` | `getMetadataReader(Resource resource)` Obtain a MetadataReader for the given resource.
    `void` | `setCacheLimit(int cacheLimit)` Specify the maximum number of entries for the MetadataReader cache.

    - ### Methods inherited from class org.springframework.core.type.classreading.SimpleMetadataReaderFactory

      `getMetadataReader, getResourceLoader`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_CACHE_LIMIT

          public static final int DEFAULT_CACHE_LIMIT

      Default maximum number of entries for a local MetadataReader cache: 256.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### CachingMetadataReaderFactory

          public CachingMetadataReaderFactory()

      Create a new CachingMetadataReaderFactory for the default class loader, using a local resource cache.

    - #### CachingMetadataReaderFactory

          public CachingMetadataReaderFactory(@Nullable
                                              ClassLoader classLoader)

      Create a new CachingMetadataReaderFactory for the given `ClassLoader`, using a local resource cache.
      Parameters:
        `classLoader` - the ClassLoader to use

    - #### CachingMetadataReaderFactory

          public CachingMetadataReaderFactory(@Nullable
                                              ResourceLoader resourceLoader)

      Create a new CachingMetadataReaderFactory for the given `ResourceLoader`, using a shared resource cache if supported or a local resource cache otherwise.
      Parameters:
        `resourceLoader` - the Spring ResourceLoader to use (also determines the ClassLoader to use)
      See Also:
        `DefaultResourceLoader.getResourceCache(java.lang.Class<T>)`

  - ### Method Detail

    - #### setCacheLimit

          public void setCacheLimit(int cacheLimit)

      Specify the maximum number of entries for the MetadataReader cache.

      Default is 256 for a local cache, whereas a shared cache is typically unbounded. This method enforces a local resource cache, even if the `ResourceLoader` supports a shared resource cache.

    - #### getCacheLimit

          public int getCacheLimit()

      Return the maximum number of entries for the MetadataReader cache.

    - #### getMetadataReader

          public MetadataReader getMetadataReader(Resource resource)
                                           throws IOException

      Description copied from interface: `MetadataReaderFactory`
      Obtain a MetadataReader for the given resource.
      Specified by:
        `getMetadataReader` in interface `MetadataReaderFactory`
      Overrides:
        `getMetadataReader` in class `SimpleMetadataReaderFactory`
      Parameters:
        `resource` - the resource (pointing to a ".class" file)
      Returns:
        a holder for the ClassReader instance (never `null`)
      Throws:
        `IOException` - in case of I/O failure

    - #### clearCache

          public void clearCache()

      Clear the local MetadataReader cache, if any, removing all cached class metadata.