利用SpringAOP手動實現一個緩衝

來源:互聯網
上載者:User

情境:物流系統庫存管理模組 : 使用者登入、倉庫增刪改查spring JdbcTemplate實現資料層操作。

為了減少DB的IO次數,配置AOP切面,用Map手動實現一個緩衝:在添加、修改、刪除倉庫時,清空緩衝;查詢倉庫列表或者查詢每個倉庫資訊時,載入資料到緩衝.

實現:

切面:

package main.com.store.aop;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import main.com.store.domain.Store;import org.aspectj.lang.ProceedingJoinPoint;/** * AOP緩衝切面 * * @author lee * */@SuppressWarnings("all")public class CacheAspect {    // 為了安全執行緒,使用Collections.synchronizedMap(new HashMap());    private static Map<String, Object> aopCahche = Collections.synchronizedMap(new HashMap<String, Object>());    // private static Map<String, Object> aopCahche = new HashMap<String, Object>();    public static Map<String, Object> getAopCahche() {        return aopCahche;    }    public static void setAopCahche(Map<String, Object> aopCahche) {        CacheAspect.aopCahche = aopCahche;    }    public static List<Store> doCacheList(ProceedingJoinPoint point) throws Throwable {        List<Store> result = (List<Store>) point.proceed();        return result;    }    public static Store doCacheSingle(ProceedingJoinPoint point) throws Throwable {        Store result = (Store) point.proceed();        return result;    }    public static void clearCache() {        aopCahche = new HashMap<String, Object>();        System.out.println("清空緩衝成功!");    }}

Dao

package main.com.store.dao;import java.util.List;import main.com.store.domain.Store;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;@SuppressWarnings("all")public class StoreDao extends HibernateDaoSupport {    /**     * 查詢所有倉庫資訊     *     * @return     */    public List<Store> findAllStores() {        List<Store> stores = this.getHibernateTemplate().find("from Store");        return stores;    }    /**     * 增加倉庫資訊     *     * @param store     */    public void add(Store store) {        this.getHibernateTemplate().save(store);    }    /**     * 刪除倉庫資訊     *     * @param id     */    public void delete(Store store) {        this.getHibernateTemplate().delete(store);    }    /**     * 根據id查詢倉庫資訊     *     * @param id     * @return     */    public Store getStoreById(String id) {        Store result = null;        result = this.getHibernateTemplate().get(Store.class, id);        return result;    }    /**     * 修改倉庫資訊     *     * @param store     */    public void update(Store store) {        this.getHibernateTemplate().update(store);    }}

VO

package main.com.store.domain;/** * 倉庫 * * @author lee * */public class Store {    /** 倉庫編號 */    private String id;    /** 倉庫名稱 */    private String name;    /** 倉庫所在地 */    private String addr;    /** 倉庫管理員 */    private String manager;    public Store() {    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAddr() {        return addr;    }    public void setAddr(String addr) {        this.addr = addr;    }    public String getManager() {        return manager;    }    public void setManager(String manager) {        this.manager = manager;    }    @Override    public String toString() {        return "Store [id=" + id + ", name=" + name + ", addr=" + addr + ", manager=" + manager + "]";    }}

Service

package main.com.store.service;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import main.com.store.aop.CacheAspect;import main.com.store.dao.StoreDao;import main.com.store.domain.Store;public class StoreService {    private StoreDao storeDao;    public void setStoreDao(StoreDao storeDao) {        this.storeDao = storeDao;    }    /**     * 查詢所有倉庫資訊     */    public List<Store> findAllStores() {        List<Store> stores = new ArrayList<Store>();        // 擷取緩衝中值,如果有        Map<String, Object> cacheMap = CacheAspect.getAopCahche();        Set<Entry<String, Object>> entrySet = cacheMap.entrySet();        if (!CacheAspect.getAopCahche().isEmpty()) {            System.out.println("查詢快取");            for (Entry<String, Object> entry : entrySet) {                String key = entry.getKey().toString();                System.out.println(key);                Store store = (Store) entry.getValue();                stores.add(store);            }        } else {            System.out.println("查詢資料庫");            stores = storeDao.findAllStores();            for (Store store : stores) {                cacheMap.put(store.getId(), store);            }        }        return stores.isEmpty() ? null : stores;    }    /**     * 根據id查詢倉庫資訊     *     * @param id     * @return     */    public Store findStoreById(String id) {        Store store = new Store();        // 擷取緩衝中值,如果有        Map<String, Object> cacheMap = CacheAspect.getAopCahche();        Set<Entry<String, Object>> entrySet = cacheMap.entrySet();        if (!cacheMap.isEmpty()) {            System.out.println("查詢快取");            for (Entry<String, Object> entry : entrySet) {                String key = entry.getKey().toString();                System.out.println(key);                store = (Store) entry.getValue();            }        } else {            System.out.println("資料庫");            store = storeDao.getStoreById(id);            cacheMap.put(store.getId(), store);        }        return store;    }    /**     * 增加倉庫     *     * @param store     */    public void add(Store store) {        storeDao.add(store);    }    /**     * 刪除倉庫資訊     *     * @param id     */    public void delete(Store store) {        storeDao.delete(store);    }    /**     * 修改倉庫資訊     *     * @param store     */    public void update(Store store) {        storeDao.update(store);    }}

applicationContext.xml

<bean id="cacheAdvice" class="main.com.store.aop.CacheAspect" /><aop:config proxy-target-class="true"><aop:aspect ref="cacheAdvice">            <aop:pointcut expression="execution(* main.com.store.service.StoreService.findAllStores(..))" id="cacheList" />            <aop:around method="doCacheList" pointcut-ref="cacheList"/>            <aop:pointcut expression="execution(* main.com.store.service.StoreService.findStoreById(..))" id="cacheSingle" />            <aop:around method="doCacheSingle" pointcut-ref="cacheSingle"/>            <aop:pointcut expression="execution(* main.com.store.service.StoreService.add(..))" id="clearAdd" />            <aop:pointcut expression="execution(* main.com.store.service.StoreService.delete(..))" id="clearDelete" />            <aop:pointcut expression="execution(* main.com.store.service.StoreService.update(..))" id="clearUpdate" />            <aop:before method="clearCache" pointcut-ref="clearAdd"/>            <aop:before method="clearCache" pointcut-ref="clearDelete"/>            <aop:before method="clearCache" pointcut-ref="clearUpdate"/>        </aop:aspect>    </aop:config><!-- 倉庫管理 -->    <bean id="storeDao" class="main.com.store.dao.StoreDao">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <bean id="storeService" class="main.com.store.service.StoreService">        <property name="storeDao" ref="storeDao" />    </bean>    <bean id="storeAction" class="main.com.store.web.StoreAction">        <property name="storeService" ref="storeService" />    </bean>

OK ! 完工!

本文出自 “狐靈傳說” 部落格,請務必保留此出處http://foxspark.blog.51cto.com/6273668/1344879

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.