【问题标题】:How to programmatically override ehcache configuration set in xml file? Or How to set CacheLoaderWriter bean to ehcache xml configuration?如何以编程方式覆盖 xml 文件中设置的 ehcache 配置?或者如何将 CacheLoaderWriter bean 设置为 ehcache xml 配置?
【发布时间】:2019-07-25 02:29:27
【问题描述】:

我想使用 Ehcache 使用 read-through 和 write-through 缓存策略,我正在使用 XML 配置来配置 Ehcache,我想设置 spring bean(CacheLoaderWriter 实现)来缓存配置,我不能使用 XML配置,因为那时 Ehcache 使用默认构造函数实例化 bean 并且 spring DI 将不起作用,所以,我如何覆盖/设置 spring 管理的 CacheLoaderWriter bean 以缓存在 java-config 中的 XML 配置文件中定义?

我尝试在我的 XML 文件中设置 CacheLoaderWriter 类,如下所示

<cache alias="employeeEntityCache">
     <key-type>java.lang.Long</key-type>
     <value-type>com.example.spring.cahce.model.Employee</value-type>
     <loader-writer>
<class>com.example.spring.cahce.stragegy.readwritethrough.EmployeeEntityLoaderWriter</class>
      </loader-writer>
      <resources>
                <heap unit="entries">100</heap>
      </resources>
</cache>

但是,然后 Ehcache 实例化 LoaderWriter bean,因此 spring DI 将无法工作

我的 java 配置,从 xml 加载缓存配置

    @Bean
    @Qualifier("jcachexml")
    public javax.cache.CacheManager jCacheCacheManager() {

        CachingProvider cachingProvider = Caching.getCachingProvider();
        try {
            javax.cache.CacheManager manager = cachingProvider.getCacheManager(
                    getClass().getResource("/ehcache-jsr107-config.xml").toURI(),
                    getClass().getClassLoader());
            return manager;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
     }

我想要一种方法来覆盖在 Ehcache XML 配置中设置的缓存配置,这样我就可以将 spring 管理的 CacheLoaderWriter bean 设置为缓存配置并可以使用 DI 注入依赖项

【问题讨论】:

    标签: spring ehcache spring-cache jcache write-through


    【解决方案1】:

    Ehcache 3.8.0 增加了新特性配置推导,我们可以使用它来覆盖配置,

    参考Configuration Derivation

    所以,我可以通过 java config 中的 XML 配置覆盖,并将 CacheLoaderWriter 设置为完全初始化的 Spring bean。

    @Autowired
    private EmployeeEntityLoaderWriter employeeEntityLoaderWriter;
    
    @Bean
    @Qualifier("jcachexml")
    public javax.cache.CacheManager jCacheCacheManager() {
       final XmlConfiguration xmlConfiguration = new XmlConfiguration(getClass().getResource("/ehcache-jsr107-config.xml"));
        CacheConfiguration modifiedEmployeeCacheConfiguration = xmlConfiguration.getCacheConfigurations().get("employeeEntityCache");
    
        // override cache configuration (specific to employee cache) from xml (Set  CacheLoderWriter)
        modifiedEmployeeCacheConfiguration = modifiedEmployeeCacheConfiguration
                .derive()
                .withLoaderWriter(employeeEntityLoaderWriter).build();
    
        // get updated configuration (at CacheManager level)
        org.ehcache.config.Configuration modifiedConfiguration = xmlConfiguration
                .derive()
                .withCache("employeeEntityCache", modifiedEmployeeCacheConfiguration)
                .build();
    
        // get CacheManager (Jcache) with above updated configuration
        final EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider();
        final javax.cache.CacheManager manager = ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), modifiedConfiguration);
    
        return manager;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2017-12-18
      • 2023-03-11
      • 2019-03-20
      相关资源
      最近更新 更多