Skip to content

Commit fd6d988

Browse files
author
eugenp
committed
cleanup work
1 parent e4a301d commit fd6d988

5 files changed

Lines changed: 83 additions & 58 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
package org.baeldung.caching.config;
2+
3+
import org.springframework.cache.annotation.EnableCaching;
4+
import org.springframework.context.annotation.Configuration;
15

26
@Configuration
37
@EnableCaching
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1+
package org.baeldung.caching.example;
12

23
public class Customer {
34

45
private int id;
56
private String name;
6-
private String customerAddress;
7+
private String address;
78

8-
public Customer(){
9+
public Customer() {
10+
super();
911
}
1012

11-
Customer(String name, String address){
13+
public Customer(final String name, final String address) {
1214
this.name = name;
13-
this.customerAddress = address;
15+
this.address = address;
1416
}
1517

18+
//
19+
1620
public int getId() {
1721
return id;
1822
}
1923

20-
public void setId(int id) {
24+
public void setId(final int id) {
2125
this.id = id;
2226
}
2327

2428
public String getName() {
2529
return name;
2630
}
27-
28-
public void setName(String name) {
31+
32+
public void setName(final String name) {
2933
this.name = name;
3034
}
3135

32-
public String getCustomerAddress() {
33-
return this.customerAddress;
36+
public String getAddress() {
37+
return address;
3438
}
3539

36-
public void setCustomerAddress(String address) {
37-
this.customerAddress = this.name + "," + address;
40+
public void setAddress(final String address) {
41+
this.address = address;
3842
}
43+
44+
public void setCustomerAddress(final String address) {
45+
this.address = name + "," + address;
46+
}
47+
3948
}

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.cache.CacheManager;
5+
import org.springframework.cache.annotation.CacheConfig;
6+
import org.springframework.cache.annotation.CacheEvict;
7+
import org.springframework.cache.annotation.CachePut;
58
import org.springframework.cache.annotation.Cacheable;
6-
import org.springframework.context.ApplicationContext;
9+
import org.springframework.cache.annotation.Caching;
710
import org.springframework.stereotype.Component;
811

912
@Component
10-
@CacheConfig("addressDemo")
13+
@CacheConfig(cacheNames = { "addressDemo" })
1114
public class CustomerDataService {
1215

1316
@Autowired
@@ -20,8 +23,8 @@ public class CustomerDataService {
2023
* @param customer the customer
2124
* @return the address
2225
*/
23-
@Cacheable("addresses", “directory”)
24-
public String getAddress1(Customer customer) {
26+
@Cacheable({ "addresses", "directory" })
27+
public String getAddress1(final Customer customer) {
2528
return customer.getAddress();
2629
}
2730

@@ -32,8 +35,8 @@ public String getAddress1(Customer customer) {
3235
* @param customer the customer
3336
* @return the address
3437
*/
35-
@CacheEvict(value="addresses", allEntries=true)
36-
public String getAddress2(Customer customer) {
38+
@CacheEvict(value = "addresses", allEntries = true)
39+
public String getAddress2(final Customer customer) {
3740
return customer.getAddress();
3841
}
3942

@@ -44,8 +47,8 @@ public String getAddress2(Customer customer) {
4447
* @param customer the customer
4548
* @return the address
4649
*/
47-
@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") })
48-
public String getAddress3(Customer customer) {
50+
@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value = "directory", key = "customer.name") })
51+
public String getAddress3(final Customer customer) {
4952
return customer.getAddress();
5053
}
5154

@@ -55,8 +58,9 @@ public String getAddress3(Customer customer) {
5558
* @param customer the customer
5659
* @return the address
5760
*/
58-
@Cacheable // parameter not required as we have declared it using @CacheConfig
59-
public String getAddress4(Customer customer) {
61+
@Cacheable
62+
// parameter not required as we have declared it using @CacheConfig
63+
public String getAddress4(final Customer customer) {
6064
return customer.getAddress();
6165
}
6266

@@ -66,9 +70,9 @@ public String getAddress4(Customer customer) {
6670
* @param customer the customer
6771
* @return the address
6872
*/
69-
@CachePut(value="addresses", condition=”#customer.name=’Tom’”)
70-
@CachePut(value="addresses", unless=”#result.length>64”)
71-
public String getAddress5(Customer customer) {
73+
@CachePut(value = "addresses", condition = "#customer.name='Tom'")
74+
// @CachePut(value = "addresses", unless = "#result.length>64")
75+
public String getAddress5(final Customer customer) {
7276
return customer.getAddress();
7377
}
7478
}
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
<beans xmlns="http://www.springframework.org/schema/beans"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns:cache="http://www.springframework.org/schema/cache"
4-
xsi:schemaLocation="
5-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6-
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:aop="http://www.springframework.org/schema/aop"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
7+
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
8+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
9+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
10+
">
711

8-
<cache:annotation-driven />
12+
<cache:annotation-driven />
913

10-
<context:annotation-config/>
11-
<bean class="org.baeldung.caching.config.myAppConfig"/>
14+
<context:annotation-config />
15+
<bean class="org.baeldung.caching.config.myAppConfig" />
1216

17+
<!-- the service that you wish to make cacheable. -->
18+
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService" />
1319

14-
<!-- the service that you wish to make cacheable.-->
15-
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
20+
<!-- define caching behavior -->
21+
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
22+
<cache:caching cache="addresses">
23+
<cache:cacheable method="getAddress" key="#customer.name" />
24+
</cache:caching>
25+
</cache:advice>
1626

17-
<!-- define caching behavior -->
18-
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
19-
<cache:caching cache="addresses">
20-
<cache:cacheable method="getAddress" key="#customer.name"/>
21-
</cache:caching>
22-
</cache:advice>
23-
24-
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
25-
<aop:config>
26-
<aop:advisor advice-ref="cachingBehavior"
27-
pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))"/>
28-
</aop:config>
27+
<!-- apply the behavior to all the implementations of CustomerDataService
28+
interface -->
29+
<aop:config>
30+
<aop:advisor advice-ref="cachingBehavior"
31+
pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))" />
32+
</aop:config>
2933

3034
</beans>
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package org.baeldung.caching.test;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
3+
import static org.junit.Assert.fail;
4+
5+
import org.baeldung.caching.example.Customer;
6+
import org.baeldung.caching.example.CustomerDataService;
7+
import org.junit.Test;
48
import org.springframework.context.ApplicationContext;
59
import org.springframework.context.support.ClassPathXmlApplicationContext;
610
import org.springframework.stereotype.Component;
7-
import static org.junit.Assert.*;
8-
import org.junit.Test;
911

1012
@Component
1113
public class SpringCachingBehaviorTest {
12-
@Test
13-
public void testCaching() {
14-
@SuppressWarnings("resource")
15-
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
16-
example = context.getBean(CustomerDataService.class);
17-
Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
18-
example.getAddress(cust);
19-
fail("Unable to instantiate the CustomerDataService");
20-
}
14+
@Test
15+
public void testCaching() {
16+
@SuppressWarnings("resource")
17+
final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
18+
final CustomerDataService service = context.getBean(CustomerDataService.class);
19+
20+
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
21+
service.getAddress1(cust);
22+
fail("Unable to instantiate the CustomerDataService");
23+
}
24+
2125
}

0 commit comments

Comments
 (0)