Java tutorial
/* *Copyright 2016 Dominik Szalai (emptulik@gmail.com) * *Licensed under the Apache License, Version 2.0 (the "License"); *you may not use this file except in compliance with the License. *You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *Unless required by applicable law or agreed to in writing, software *distributed under the License is distributed on an "AS IS" BASIS, *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *See the License for the specific language governing permissions and *limitations under the License. */ package cz.muni.fi.editor.services.commons; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.cache.CacheManager; import org.springframework.cache.jcache.JCacheCacheManager; import org.springframework.stereotype.Component; import javax.cache.Caching; import javax.cache.configuration.MutableConfiguration; import javax.cache.spi.CachingProvider; /** * Created by Dominik Szalai - emptulik at gmail.com on 21.11.2016. */ @Log4j2 @Component public class CacheManagerFactoryBean implements FactoryBean<CacheManager>, InitializingBean { private CacheManager cacheManager; @Override public CacheManager getObject() throws Exception { log.info("get manager"); return cacheManager; } @Override public Class<?> getObjectType() { return cacheManager != null ? cacheManager.getClass() : CacheManager.class; } @Override public boolean isSingleton() { return true; } @Override public void afterPropertiesSet() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager( getClass().getResource("/config/cache.xml").toURI(), provider.getDefaultClassLoader()); MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); configuration.setStoreByValue(false); /** * avoids dirties context reset of cache, where cache already exists */ for (String s : new String[] { "userCache", "organizationCache", "organizationCacheList" }) { if (cacheManager.getCache(s) == null) { cacheManager.createCache(s, configuration); } } this.cacheManager = new JCacheCacheManager(cacheManager); } }