zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_EhCache

    <?xml version="1.0" encoding="GBK"?>
    <project name="spring" basedir="." default="">
    	<property name="src" value="src"/>
    	<property name="dest" value="classes"/>
    
    	<path id="classpath">
    		<fileset dir="../../lib">
    			<include name="**/*.jar"/>
    		</fileset>
    		<pathelement path="${dest}"/>
    	</path>
    
    	<target name="compile" description="Compile all source code">
    		<delete dir="${dest}"/>
    		<mkdir dir="${dest}"/>
    		<copy todir="${dest}">
    			<fileset dir="${src}">
    				<exclude name="**/*.java"/>
    			</fileset>		
    		</copy>
    		<javac destdir="${dest}" debug="true" includeantruntime="yes"
    			deprecation="false" optimize="false" failonerror="true">
    			<src path="${src}"/>
    			<classpath refid="classpath"/>
    			<compilerarg value="-Xlint:deprecation"/>
    		</javac>
    	</target>
    
    	<target name="run" description="Run the main class" depends="compile">
    		<java classname="lee.SpringTest" fork="yes" failonerror="true">
    			<classpath refid="classpath"/>
    		</java>
    	</target>
    
    </project>
    

      

    <?xml version="1.0" encoding="gbk"?>
    <ehcache>
        <diskStore path="java.io.tmpdir" />
        <!-- 配置默认的缓存区 -->
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"/>
        <!-- 配置名为users的缓存区 -->
        <cache name="users"
            maxElementsInMemory="10000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="300"
            timeToLiveSeconds="600" />
    </ehcache>
    <?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
    	通过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>
    

      

    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;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author Yeeku.H.Lee kongyeeku@163.com
     * @version 1.0
     */
    public class SpringTest
    {
    	public static void main(String[] args)
    	{
    		ApplicationContext ctx =
    			new ClassPathXmlApplicationContext("beans.xml");
    		UserService us = ctx.getBean("userService" , UserService.class);
    		// 第一次调用us对象的方法时会执行该方法,并缓存方法的结果
    		User u1 = us.getUsersByNameAndAge("孙悟空", 500);
    		// 第二次调用us对象的方法时直接利用缓存的数据,并不真正执行该方法
    		User u2 = us.getAnotherUser("孙悟空", 500);
    		System.out.println(u1 == u2); // 输出true
    	}
    }
    

      

    package org.crazyit.app.domain;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author Yeeku.H.Lee kongyeeku@163.com
     * @version 1.0
     */
    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;
        }
        
        
    
    }
    package org.crazyit.app.service;
    
    import org.crazyit.app.domain.User;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author Yeeku.H.Lee kongyeeku@163.com
     * @version 1.0
     */
    public interface UserService
    {
        User getUsersByNameAndAge(String name, int age);
    
        User getAnotherUser(String name, int age);
    }
    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;
    
    /**
     * Description: <br/>
     * 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> <br/>
     * Copyright (C), 2001-2014, Yeeku.H.Lee <br/>
     * This program is protected by copyright laws. <br/>
     * Program Name: <br/>
     * Date:
     * @author Yeeku.H.Lee kongyeeku@163.com
     * @version 1.0
     */
    @Service("userService")
    @Cacheable(value = "users")
    public class UserServiceImpl implements UserService
    {
        public User getUsersByNameAndAge(String name, int age)
        {
            System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
            return new User(name, age);
        }
        public User getAnotherUser(String name, int age)
        {
            System.out.println("--正在执行findAnotherUser()查询方法--");
            return new User(name, age);
        }
    }
  • 相关阅读:
    覆盖一个DIV,让radio、checkbox和select只读
    DNN 配置 数据库篇
    Javascript实用语句收录
    绑定Hashtable到DataList
    WebDeploymentSetup 合并程序集时出错(ILMerge.Merge: ERROR)
    只能向页面中添加 ScriptManager 的一个实例
    让DNN添加的别名起作用
    360极速模式(Chrome内核)下由ashx输出的JavaScript代码不起作用
    C#中常用到的时间函数(天数差、星期几等)
    C#实用语句
  • 原文地址:https://www.cnblogs.com/tszr/p/12372612.html
Copyright © 2011-2022 走看看