caching java config

This commit is contained in:
DOHA 2015-12-06 13:01:52 +02:00
parent de70a8595c
commit f54c2fcae4
6 changed files with 48 additions and 21 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<faceted-project> <faceted-project>
<installed facet="wst.jsdt.web" version="1.0"/> <installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="java" version="1.8"/> <installed facet="java" version="1.8"/>
<installed facet="jst.web" version="3.1"/>
</faceted-project> </faceted-project>

View File

@ -0,0 +1,29 @@
package org.baeldung.caching.config;
import java.util.Arrays;
import org.baeldung.caching.example.CustomerDataService;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CachingConfig {
@Bean
public CustomerDataService customerDataService() {
return new CustomerDataService();
}
@Bean
public CacheManager cacheManager() {
final SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("directory"), new ConcurrentMapCache("addresses")));
return cacheManager;
}
}

View File

@ -1,10 +0,0 @@
package org.baeldung.caching.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class MyAppConfig {
// Your configuration code goes here.
}

View File

@ -10,12 +10,18 @@ import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@CacheConfig(cacheNames = { "addressDemo" }) @CacheConfig(cacheNames = { "addresses" })
public class CustomerDataService { public class CustomerDataService {
@Autowired @Autowired
CacheManager cacheManager; CacheManager cacheManager;
// this method configuration is equivalent to xml configuration
@Cacheable(value = "addresses", key = "#customer.name")
public String getAddress(final Customer customer) {
return customer.getAddress();
}
/** /**
* The method returns the customer's address, * The method returns the customer's address,
only it doesn't find it the cache- addresses and directory. only it doesn't find it the cache- addresses and directory.

View File

@ -10,8 +10,7 @@
<cache:annotation-driven/> <cache:annotation-driven/>
<context:annotation-config/> <!-- <context:annotation-config/> -->
<bean class="org.baeldung.caching.config.MyAppConfig"/>
<!-- the service that you wish to make cacheable. --> <!-- the service that you wish to make cacheable. -->
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/> <bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
@ -21,7 +20,6 @@
<set> <set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="directory"/> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="directory"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addresses"/> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addresses"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addressDemo"/>
</set> </set>
</property> </property>
</bean> </bean>
@ -34,7 +32,7 @@
</cache:advice> </cache:advice>
<!-- apply the behavior to all the implementations of CustomerDataService interface --> <!-- apply the behavior to all the implementations of CustomerDataService -->
<aop:config> <aop:config>
<aop:advisor advice-ref="cachingBehavior" pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))"/> <aop:advisor advice-ref="cachingBehavior" pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))"/>
</aop:config> </aop:config>

View File

@ -1,11 +1,11 @@
package org.baeldung.caching.test; package org.baeldung.caching.test;
import org.baeldung.caching.config.CachingConfig;
import org.baeldung.caching.example.Customer; import org.baeldung.caching.example.Customer;
import org.baeldung.caching.example.CustomerDataService; import org.baeldung.caching.example.CustomerDataService;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@Component @Component
public class SpringCachingBehaviorTest { public class SpringCachingBehaviorTest {
@ -13,12 +13,16 @@ public class SpringCachingBehaviorTest {
@Test @Test
public void testCaching() { public void testCaching() {
@SuppressWarnings("resource") @SuppressWarnings("resource")
final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); final
// final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(CachingConfig.class);
context.refresh();
final CustomerDataService service = context.getBean(CustomerDataService.class); final CustomerDataService service = context.getBean(CustomerDataService.class);
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
service.getAddress1(cust); service.getAddress(cust);
service.getAddress1(cust); service.getAddress(cust);
// fail("Unable to instantiate the CustomerDataService"); // fail("Unable to instantiate the CustomerDataService");
} }