這篇文章給大家介紹Spring中condition如何使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站頁(yè)面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)站建設(shè)公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁(yè)面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠(chéng)信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場(chǎng)環(huán)境中,互促共生。
一 配置文件
<?xml version="1.0" encoding="GBK"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="org.crazyit.app.service" /> <cache:annotation-driven cache-manager="cacheManager" /> <!-- 配置EhCache的CacheManager 通過(guò)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); }}
五 測(cè)試類
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,因此不會(huì)緩存, // 所以下面兩次方法調(diào)用都會(huì)真正執(zhí)行這些方法。 User u1 = us.getUsersByNameAndAge("孫悟空", 500); User u2 = us.getAnotherUser("孫悟空", 500); System.out.println(u1 == u2); // 輸出false // 調(diào)用方法時(shí)age參數(shù)小于100,因此會(huì)緩存, // 所以下面第二次方法調(diào)用時(shí)不會(huì)真正執(zhí)行該方法,而是直接使用緩存數(shù)據(jù)。 User u3 = us.getUsersByNameAndAge("孫悟空", 50); User u4 = us.getAnotherUser("孫悟空", 50); System.out.println(u3 == u4); // 輸出true }}
六 測(cè)試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法----正在執(zhí)行findAnotherUser()查詢方法--false--正在執(zhí)行findUsersByNameAndAge()查詢方法--true
關(guān)于Spring中condition如何使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
本文標(biāo)題:Spring中condition如何使用
網(wǎng)站地址:http://chinadenli.net/article8/ggjhip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、微信小程序、營(yíng)銷型網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、網(wǎng)站維護(hù)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)