caching java config
This commit is contained in:
parent
de70a8595c
commit
f54c2fcae4
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="wst.jsdt.web" version="1.0"/>
|
||||
<installed facet="jst.web" version="3.0"/>
|
||||
<installed facet="java" version="1.8"/>
|
||||
<installed facet="jst.web" version="3.1"/>
|
||||
</faceted-project>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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.
|
||||
}
|
|
@ -10,12 +10,18 @@ import org.springframework.cache.annotation.Caching;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@CacheConfig(cacheNames = { "addressDemo" })
|
||||
@CacheConfig(cacheNames = { "addresses" })
|
||||
public class CustomerDataService {
|
||||
|
||||
@Autowired
|
||||
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,
|
||||
only it doesn't find it the cache- addresses and directory.
|
||||
|
|
|
@ -10,8 +10,7 @@
|
|||
|
||||
<cache:annotation-driven/>
|
||||
|
||||
<context:annotation-config/>
|
||||
<bean class="org.baeldung.caching.config.MyAppConfig"/>
|
||||
<!-- <context:annotation-config/> -->
|
||||
|
||||
<!-- the service that you wish to make cacheable. -->
|
||||
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
|
||||
|
@ -21,7 +20,6 @@
|
|||
<set>
|
||||
<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="addressDemo"/>
|
||||
</set>
|
||||
</property>
|
||||
</bean>
|
||||
|
@ -34,7 +32,7 @@
|
|||
</cache:advice>
|
||||
|
||||
|
||||
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
|
||||
<!-- apply the behavior to all the implementations of CustomerDataService -->
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="cachingBehavior" pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))"/>
|
||||
</aop:config>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package org.baeldung.caching.test;
|
||||
|
||||
import org.baeldung.caching.config.CachingConfig;
|
||||
import org.baeldung.caching.example.Customer;
|
||||
import org.baeldung.caching.example.CustomerDataService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
@Component
|
||||
public class SpringCachingBehaviorTest {
|
||||
|
@ -13,12 +13,16 @@ public class SpringCachingBehaviorTest {
|
|||
@Test
|
||||
public void testCaching() {
|
||||
@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 Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
|
||||
service.getAddress1(cust);
|
||||
service.getAddress1(cust);
|
||||
service.getAddress(cust);
|
||||
service.getAddress(cust);
|
||||
|
||||
// fail("Unable to instantiate the CustomerDataService");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue