티스토리 뷰

spring의 version이 올라가면서 모든 설정은 java config로 하기를 권장하고 있다. 이에 따라서 관련된 글도 포스팅도 여러개 했었다. 

 

Spring 설정 Xml Config에서 Java Config로 바꾸는 방법 (요령)

spring 프로젝트에서 springboot 프로젝트로 migration 하기 (6) - XML config to JAVA config spring 프로젝트에서 springboot 프로젝트로 migration 하기 (1) - pom.xml spring 프로젝트에서 springboot 프로젝..

oingdaddy.tistory.com

 

 

spring 프로젝트에서 springboot 프로젝트로 migration 하기 (6) - XML config to JAVA config

spring 프로젝트에서 springboot 프로젝트로 migration 하기 (1) - pom.xml spring 프로젝트에서 springboot 프로젝트로 migration 하기 (2) - web.xml spring 프로젝트에서 springboot 프로젝트로 migration 하기..

oingdaddy.tistory.com

EhCache도 프로젝트에 적용을 했는데 xml config로 되어 있어서 이도 java config로 전환하는 작업을 진행하였다. 


AS-IS (xml config)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    
    <diskStore path="java.io.tmpdir" />
    
    <cache name="getMenu"
           maxEntriesLocalHeap="1000"  
           maxEntriesLocalDisk="10000" 
           eternal="false"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="1800" timeToLiveSeconds="1800"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
           <persistence strategy="localTempSwap" />
    </cache>
    
</ehcache>

xml config는 위와 같은 모습이다. EhCache에 대한 설정에 대한 정보는 이 글을 참조하도록 하자. 

 

TO-BE (java config)

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.CacheConfiguration.TransactionalMode;
import net.sf.ehcache.config.DiskStoreConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration;

@EnableCaching
@Configuration
public class CacheConfig {

    private net.sf.ehcache.CacheManager createCacheManager() {
        net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
        configuration.diskStore(new DiskStoreConfiguration().path("java.io.tmpdir"));
        return net.sf.ehcache.CacheManager.create(configuration);
    }

    @Bean
    public EhCacheCacheManager ehCacheCacheManager() {
        
        net.sf.ehcache.CacheManager manager = this.createCacheManager();

        Cache getMenuCache = new Cache(new CacheConfiguration()
                .maxEntriesLocalHeap(1000)           
                .maxEntriesLocalDisk(10000)
                .eternal(false)                     
                .timeToIdleSeconds(1800)            
                .timeToLiveSeconds(1800)            
                .memoryStoreEvictionPolicy("LFU") 
                .transactionalMode(TransactionalMode.OFF)
                .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP))
                .name("getMenu")
        );
        manager.addCache(getMenuCache);
        
        return new EhCacheCacheManager(manager);
    }
}

이런식으로 xml config를 java config로 변환할 수 있다. 기존에는 CacheConfig 클래스를 만들고 단순히 @EnableCaching 만 달아주었다면 이곳에 EhCache에 관련된 설정을 해주었다. 이렇게 설정을 했다면 물론 기존 설정했던 xml config 파일은 지워도 된다. cache의 property 값은 이 글을 참조하도록 하자.  

 

끝!

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31