Skip to content

Commit f54c2fc

Browse files
committed
caching java config
1 parent de70a85 commit f54c2fc

6 files changed

Lines changed: 48 additions & 21 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<faceted-project>
33
<installed facet="wst.jsdt.web" version="1.0"/>
4-
<installed facet="jst.web" version="3.0"/>
54
<installed facet="java" version="1.8"/>
5+
<installed facet="jst.web" version="3.1"/>
66
</faceted-project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.caching.config;
2+
3+
import java.util.Arrays;
4+
5+
import org.baeldung.caching.example.CustomerDataService;
6+
import org.springframework.cache.CacheManager;
7+
import org.springframework.cache.annotation.EnableCaching;
8+
import org.springframework.cache.concurrent.ConcurrentMapCache;
9+
import org.springframework.cache.support.SimpleCacheManager;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
@Configuration
14+
@EnableCaching
15+
public class CachingConfig {
16+
17+
@Bean
18+
public CustomerDataService customerDataService() {
19+
return new CustomerDataService();
20+
}
21+
22+
@Bean
23+
public CacheManager cacheManager() {
24+
final SimpleCacheManager cacheManager = new SimpleCacheManager();
25+
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("directory"), new ConcurrentMapCache("addresses")));
26+
return cacheManager;
27+
}
28+
29+
}

‎spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
import org.springframework.stereotype.Component;
1111

1212
@Component
13-
@CacheConfig(cacheNames = { "addressDemo" })
13+
@CacheConfig(cacheNames = { "addresses" })
1414
public class CustomerDataService {
1515

1616
@Autowired
1717
CacheManager cacheManager;
1818

19+
// this method configuration is equivalent to xml configuration
20+
@Cacheable(value = "addresses", key = "#customer.name")
21+
public String getAddress(final Customer customer) {
22+
return customer.getAddress();
23+
}
24+
1925
/**
2026
* The method returns the customer's address,
2127
only it doesn't find it the cache- addresses and directory.

‎spring-all/src/main/resources/config.xml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
<cache:annotation-driven/>
1212

13-
<context:annotation-config/>
14-
<bean class="org.baeldung.caching.config.MyAppConfig"/>
13+
<!-- <context:annotation-config/> -->
1514

1615
<!-- the service that you wish to make cacheable. -->
1716
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
@@ -21,7 +20,6 @@
2120
<set>
2221
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="directory"/>
2322
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addresses"/>
24-
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addressDemo"/>
2523
</set>
2624
</property>
2725
</bean>
@@ -34,7 +32,7 @@
3432
</cache:advice>
3533

3634

37-
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
35+
<!-- apply the behavior to all the implementations of CustomerDataService -->
3836
<aop:config>
3937
<aop:advisor advice-ref="cachingBehavior" pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))"/>
4038
</aop:config>

‎spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package org.baeldung.caching.test;
22

3+
import org.baeldung.caching.config.CachingConfig;
34
import org.baeldung.caching.example.Customer;
45
import org.baeldung.caching.example.CustomerDataService;
56
import org.junit.Test;
6-
import org.springframework.context.ApplicationContext;
7-
import org.springframework.context.support.ClassPathXmlApplicationContext;
87
import org.springframework.stereotype.Component;
8+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
99

1010
@Component
1111
public class SpringCachingBehaviorTest {
1212

1313
@Test
1414
public void testCaching() {
1515
@SuppressWarnings("resource")
16-
final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
16+
final
17+
// final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
18+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
19+
context.register(CachingConfig.class);
20+
context.refresh();
1721
final CustomerDataService service = context.getBean(CustomerDataService.class);
1822

1923
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
20-
service.getAddress1(cust);
21-
service.getAddress1(cust);
24+
service.getAddress(cust);
25+
service.getAddress(cust);
2226

2327
// fail("Unable to instantiate the CustomerDataService");
2428
}

0 commit comments

Comments
 (0)