Updated the caching behavior code:
This commit is contained in:
		
							parent
							
								
									57585f9b2c
								
							
						
					
					
						commit
						df2f1cbaf8
					
				| @ -0,0 +1,9 @@ | |||||||
|  | 
 | ||||||
|  | @Configuration | ||||||
|  | @EnableCaching | ||||||
|  | 
 | ||||||
|  | public class myAppConfig { | ||||||
|  | 
 | ||||||
|  |     // Your configuration code goes here. | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,74 @@ | |||||||
|  | /** | ||||||
|  |  * The Class Customer. | ||||||
|  |  */ | ||||||
|  | public class Customer { | ||||||
|  | 
 | ||||||
|  |   /** The id. */ | ||||||
|  |   private int id; | ||||||
|  | 
 | ||||||
|  |   /** The name. */ | ||||||
|  |   private String name; | ||||||
|  | 
 | ||||||
|  |   /** The address. */ | ||||||
|  |   private String customerAddress; | ||||||
|  | 
 | ||||||
|  |   Customer(String name, String address){ | ||||||
|  |     this.name = name; | ||||||
|  |     this.address = address; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets the id. | ||||||
|  |    * | ||||||
|  |    * @return the id | ||||||
|  |    */ | ||||||
|  |   public int getId() { | ||||||
|  |     return id; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Sets the id. | ||||||
|  |    * | ||||||
|  |    * @param id the new id | ||||||
|  |    */ | ||||||
|  |   public void setId(int id) { | ||||||
|  |     this.id = id; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets the name. | ||||||
|  |    * | ||||||
|  |    * @return the name | ||||||
|  |    */ | ||||||
|  |   public String getName() { | ||||||
|  |     return name; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Gets the address. | ||||||
|  |    * | ||||||
|  |    * @return the address | ||||||
|  |    */ | ||||||
|  |   public String getCustomerAddress() { | ||||||
|  |     return this.customerAddress; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Sets the name. | ||||||
|  |    * | ||||||
|  |    * @param name the new name | ||||||
|  |    */ | ||||||
|  |   public void setName(String name) { | ||||||
|  |     this.name = name; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Sets the address. | ||||||
|  |    * | ||||||
|  |    * @param name the new address | ||||||
|  |    */ | ||||||
|  |   public void setCustomerAddress(String address) { | ||||||
|  |     this.customerAddress = this.name + "," + address; | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,93 @@ | |||||||
|  | package org.baeldung.caching.example; | ||||||
|  | 
 | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cache.annotation.Cacheable; | ||||||
|  | import org.springframework.context.ApplicationContext; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * The Class CustomerDataService. | ||||||
|  |  */ | ||||||
|  | @Component | ||||||
|  | @CacheConfig("addressDemo") | ||||||
|  | public class CustomerDataService { | ||||||
|  | 
 | ||||||
|  | 	/** The cache manager. */ | ||||||
|  | 	@Autowired | ||||||
|  | 	CacheManager cacheManager; | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Gets the address. | ||||||
|  | 	 * | ||||||
|  | 	 * @param customer the customer | ||||||
|  | 	 * @return the address | ||||||
|  | 	 */ | ||||||
|  | 
 | ||||||
|  |    @Cacheable("addresses", “directory”) | ||||||
|  | 
 | ||||||
|  |    public String getAddress1(Customer customer) { | ||||||
|  | 
 | ||||||
|  |      return customer.getAddress(); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 		* Gets the address. | ||||||
|  | 		* | ||||||
|  | 		* @param customer the customer | ||||||
|  | 		* @return the address | ||||||
|  | 		*/ | ||||||
|  | 
 | ||||||
|  | 	@CacheEvict(value="addresses", allEntries=true) | ||||||
|  | 
 | ||||||
|  | 	public String getAddress2(Customer customer) { | ||||||
|  | 
 | ||||||
|  | 		return customer.getAddress(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 		* Gets the address. | ||||||
|  | 		* | ||||||
|  | 		* @param customer the customer | ||||||
|  | 		* @return the address | ||||||
|  | 		*/ | ||||||
|  | 
 | ||||||
|  | 	@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) | ||||||
|  | 
 | ||||||
|  | 	public String getAddress3(Customer customer) { | ||||||
|  | 
 | ||||||
|  | 		return customer.getAddress(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	* Gets the address. | ||||||
|  | 	* | ||||||
|  | 	* @param customer the customer | ||||||
|  | 	* @return the address | ||||||
|  | 	*/ | ||||||
|  | 
 | ||||||
|  | 	@Cacheable // parameter not required as we have declared it using @CacheConfig | ||||||
|  | 
 | ||||||
|  | 	public String getAddress4(Customer customer) { | ||||||
|  | 
 | ||||||
|  | 		return customer.getAddress(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	* Gets the address. | ||||||
|  | 	* | ||||||
|  | 	* @param customer the customer | ||||||
|  | 	* @return the address | ||||||
|  | 	*/ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 	@CachePut(value="addresses", condition=”#customer.name=’Tom’”) | ||||||
|  | 	@CachePut(value="addresses", unless=”#result.length>64”) | ||||||
|  | 
 | ||||||
|  | 	public String getAddress5(Customer customer) { | ||||||
|  | 
 | ||||||
|  | 		return customer.getAddress(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | <beans xmlns="http://www.springframework.org/schema/beans" | ||||||
|  |     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
|  |     xmlns:cache="http://www.springframework.org/schema/cache" | ||||||
|  |     xsi:schemaLocation=" | ||||||
|  |         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||||||
|  |         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> | ||||||
|  | 
 | ||||||
|  |         <cache:annotation-driven /> | ||||||
|  | 
 | ||||||
|  |            <context:annotation-config/> | ||||||
|  |            <bean class="org.baeldung.caching.config.myAppConfig"/> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         <!-- the service that you wish to make cacheable.--> | ||||||
|  |        <bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/> | ||||||
|  | 
 | ||||||
|  |        <!-- define caching behavior --> | ||||||
|  |        <cache:advice id="cachingBehavior" cache-manager="cacheManager"> | ||||||
|  |            <cache:caching cache="addresses"> | ||||||
|  |                <cache:cacheable method="getAddress" key="#customer.name"/> | ||||||
|  |            </cache:caching> | ||||||
|  |        </cache:advice> | ||||||
|  | 
 | ||||||
|  |        <!-- apply the behavior to all the implementations of CustomerDataService interface --> | ||||||
|  |        <aop:config> | ||||||
|  |            <aop:advisor advice-ref="cachingBehavior" | ||||||
|  |        pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))"/> | ||||||
|  |        </aop:config> | ||||||
|  | 
 | ||||||
|  | </beans> | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | package org.baeldung.caching.test; | ||||||
|  | 
 | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.context.ApplicationContext; | ||||||
|  | import org.springframework.context.support.ClassPathXmlApplicationContext; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * The Class CustomerDataService. | ||||||
|  |  */ | ||||||
|  | @Component | ||||||
|  | 
 | ||||||
|  | public class SpringCachingBehaviorTest { | ||||||
|  | /** | ||||||
|  |  * The main method. | ||||||
|  |  * | ||||||
|  |  * @param args the arguments | ||||||
|  |  */ | ||||||
|  |  public static void main(String[] args) { | ||||||
|  |     @SuppressWarnings("resource") | ||||||
|  |     ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); | ||||||
|  |     example = context.getBean(CustomerDataService.class); | ||||||
|  |     Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); | ||||||
|  |     example.getAddress(cust); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user