這篇文章給大家介紹Spring中condition如何使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
一 配置文件
<?xml version="1.0" encoding="GBK"?><beans xmlns="/tupian/20230522/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="/tupian/20230522/" xmlns:context="/tupian/20230522/" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//spring-beans-4.0.xsd /tupian/20230522/ /tupian/20230522//spring-cache-4.0.xsd /tupian/20230522/ /tupian/20230522//spring-context-4.0.xsd"> <context:component-scan base-package="org.crazyit.app.service" /> <cache:annotation-driven cache-manager="cacheManager" /> <!-- 配置EhCache的CacheManager 通過configLocation指定ehcache.xml文件的位置 --> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" p:shared="false" /> <!-- 配置基于EhCache的緩存管理器 并將EhCache的CacheManager注入該緩存管理器Bean --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManager"> </bean></beans>
二 屬性文件
<?xml version="1.0" encoding="gbk"?><ehcache> <diskStore path="java.io.tmpdir" /> <!-- 配置默認(rèn)的緩存區(qū) --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/> <!-- 配置名為users的緩存區(qū) --> <cache name="users" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" /></ehcache>
三 領(lǐng)域模型
package org.crazyit.app.domain;public class User{ private String name; private int age; public User() {} public User(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
四 Service
1 接口類
package org.crazyit.app.service;import org.crazyit.app.domain.User;public interface UserService{ User getUsersByNameAndAge(String name, int age); User getAnotherUser(String name, int age);}
2 實(shí)現(xiàn)類
package org.crazyit.app.service.impl;import org.crazyit.app.service.UserService;import org.crazyit.app.domain.User;import org.springframework.stereotype.Service;import org.springframework.cache.annotation.Cacheable;import org.springframework.context.annotation.Scope;@Service("userService")@Cacheable(value = "users" , condition="#age<100")public class UserServiceImpl implements UserService{ public User getUsersByNameAndAge(String name, int age) { System.out.println("--正在執(zhí)行findUsersByNameAndAge()查詢方法--"); return new User(name, age); } public User getAnotherUser(String name, int age) { System.out.println("--正在執(zhí)行findAnotherUser()查詢方法--"); return new User(name, age); }}
五 測試類
package lee;import org.crazyit.app.service.UserService;import org.crazyit.app.domain.User;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest{ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService us = ctx.getBean("userService" , UserService.class); // 調(diào)用方法時(shí)age參數(shù)不小于100,因此不會緩存, // 所以下面兩次方法調(diào)用都會真正執(zhí)行這些方法。 User u1 = us.getUsersByNameAndAge("孫悟空", 500); User u2 = us.getAnotherUser("孫悟空", 500); System.out.println(u1 == u2); // 輸出false // 調(diào)用方法時(shí)age參數(shù)小于100,因此會緩存, // 所以下面第二次方法調(diào)用時(shí)不會真正執(zhí)行該方法,而是直接使用緩存數(shù)據(jù)。 User u3 = us.getUsersByNameAndAge("孫悟空", 50); User u4 = us.getAnotherUser("孫悟空", 50); System.out.println(u3 == u4); // 輸出true }}
六 測試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法----正在執(zhí)行findAnotherUser()查詢方法--false--正在執(zhí)行findUsersByNameAndAge()查詢方法--true
關(guān)于Spring中condition如何使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
名稱欄目:Spring中condition如何使用-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://aaarwkj.com/article36/ccjjsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、虛擬主機(jī)、營銷型網(wǎng)站建設(shè)、面包屑導(dǎo)航、標(biāo)簽優(yōu)化、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容