- 浏览: 1723140 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (337)
- javaScript校验 (11)
- java (31)
- java连接数据库 (1)
- js应用 (41)
- JQuery (15)
- linux操作命令 (12)
- loadrunner测试 (5)
- tomcat (18)
- 数据库 (12)
- eclipse (7)
- 触发器 (7)
- 表单 (3)
- ibatis (6)
- oracle (4)
- xml (1)
- Exception (6)
- spring (16)
- struts (4)
- struts 标签 (2)
- sql (8)
- sql server (6)
- 其它 (18)
- Apache (2)
- 电脑故障 (4)
- java 线程 (1)
- dwr (8)
- jackey (18)
- 总结 (34)
- gcc linux (2)
- extjs 学习 (5)
- 网站建设 (4)
- 健康 (2)
- 房地产知识 (1)
- hibernate (3)
- mysql (7)
- linux (13)
- svn (1)
最新评论
-
阳光泛滥的日子:
很好用谢谢
java.net.URISyntaxException的解决办法 -
linjianqing:
现在有更好的处理方式吗?我正也为这发愁
applet访问打印机出现的问题 -
ruyi574812039:
非常感谢您的总结!想问几个问题!假设三个项目分别以静态部署的三 ...
在Tomcat中部署Web程序的几种方式 -
yangguo:
太j8麻烦了
Spring3.1中使用缓存注解及Shiro的缓存联合 -
bellawang:
很好用谢谢
java.net.URISyntaxException的解决办法
Spring最近释出了3.1的REALEASE版本,到我写这篇日志的时候已经是3.1.2.REALEASE版本了,该版本直接内置了Ehcache的缓存注解,比起以前配置上是容易了许多,但是shiro官方却并没有为此3.1版本的注解缓存更新其最新的实现方式,为了能够用上最新版本的spring和shiro(1.2.1),特别针对缓存部分做了一些修改。
首先Spring 3.1及以后版本的cache功能位于spring-context包中,要使用spring3.1的缓存注解,只需要在spring的主配置xml文件中添加schema:
1
|
xmlns:cache="http://www.springframework.org/schema/cache"
|
记得在xsi:schemaLocation中增加:
1
|
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
|
注意:cache的schemaLocation暂时不支持不写版本号的xsd,也就是说,现在还必须写上spring-cache-3.1.xsd
然后写上
1
|
<cache:annotation-driven/>
|
就可以使用注解@Cacheable和@CacheEvict了
@Cacheable:要缓存的方法或者类,属性:value、key、condition,value是最主要的,对应ehcache.xml中声明的cache的name;key的主要作用我认为是给局部更新缓存使用的,并且支持SpEL;condition是触发条件,空表示全部增加,也支持SpEL。
@CacheEvict:要进行清空缓存的方法或者类,属性:value、key、condition、allEntries、beforeInvocation,前三者与@Cacheable类似,allEntries为true表示清空全部缓存,默认为false;beforeInvocation为true表示在方法执行以前清理缓存,默认为false
当然,如果想要详细的配置,就需要在xml中添加一些东西了:
1
2 3 4 5 6 7 8 9 10 11 12 13 |
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:/config/ehcache.xml" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory" /> </bean> |
以上Spring 3.1的缓存注解就已经OK了,接下去就是Shiro的部分。
shiro目前还不支持直接使用上述Spring 3.1的配置,但是我们可以改造一下:
首先得写两个类进行注入:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
/**
* */ package org.apache.shiro.cache.ehcacheSpring3_1; import java.io.IOException; import java.io.InputStream; import org.apache.shiro.ShiroException; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.cache.CacheManager; import org.apache.shiro.config.ConfigurationException; import org.apache.shiro.io.ResourceUtils; import org.apache.shiro.util.Destroyable; import org.apache.shiro.util.Initializable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.ehcache.EhCacheCacheManager; /** * @author Yockii * */ public class EhCacheManager implements CacheManager, Initializable, Destroyable { private static final Logger log = LoggerFactory.getLogger(EhCacheManager.class); protected org.springframework.cache.ehcache.EhCacheCacheManager manager; private boolean cacheManagerImplicitlyCreated = false; private String cacheManagerConfigFile = "classpath:org/apache/shiro/cache/ehcache/ehcache.xml"; public EhCacheManager(){} public org.springframework.cache.ehcache.EhCacheCacheManager getCacheManager(){ return manager; } public void setCacheManager(org.springframework.cache.ehcache.EhCacheCacheManager manager){ this.manager = manager; } public String getCacheManagerConfigFile(){ return this.cacheManagerConfigFile; } public void setCacheManagerConfigFile(String classpathLocation){ this.cacheManagerConfigFile = classpathLocation; } protected InputStream getCacheManagerConfigFileInputStream(){ String configFile = getCacheManagerConfigFile(); try{ return ResourceUtils.getInputStreamForPath(configFile); } catch (IOException e) { throw new ConfigurationException("Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e); } } /* (non-Javadoc) * @see org.apache.shiro.util.Destroyable#destroy() */ @Override public void destroy() throws Exception { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.apache.shiro.util.Initializable#init() */ @Override public void init() throws ShiroException { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.apache.shiro.cache.CacheManager#getCache(java.lang.String) */ @Override public final <K, V> Cache<K, V> getCache(String name) throws CacheException { if(log.isTraceEnabled()){ log.trace("Acquiring EhCache instance named [" + name + "]"); } try{ org.springframework.cache.Cache cache = manager.getCache(name); if(cache == null){ if(log.isInfoEnabled()){ log.info("Cache with name '{}' does not yet exist. Creating now.", name); } this.manager.getCacheManager().addCache(name); cache = manager.getCache(name); if(log.isInfoEnabled()){ log.info("Added EhCache named [" + name + "]"); } } else { if(log.isInfoEnabled()){ log.info("Using existing EHCache named [" + cache.getName() + "]"); } } return new EhCache<K, V>(cache); } catch (Exception e) { throw new CacheException(e); } } private EhCacheCacheManager ensureCacheManager(){ try{ if(this.manager == null){ if(log.isDebugEnabled()){ log.debug("cacheManager property not set. Constructing CacheManager instance... "); } this.manager = new org.springframework.cache.ehcache.EhCacheCacheManager(); manager.setCacheManager(new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream())); if(log.isTraceEnabled()){ log.trace("instantiated Ehcache CacheManager instance WITH SPRING EhCacheCacheManager"); } cacheManagerImplicitlyCreated = true; if(log.isDebugEnabled()){ log.debug("implicit cacheManager created WITH SPRING EhCacheCacheManager successfully."); } } return this.manager; } catch (Exception e) { throw new CacheException(e); } } } |
这个是主要的缓存管理类,还有一个缓存类hack类Ehcache
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
/**
* */ package org.apache.shiro.cache.ehcacheSpring3_1; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import net.sf.ehcache.Ehcache; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.util.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.Cache.ValueWrapper; /** * @author Yockii * */ public class EhCache<K, V> implements Cache<K, V> { private static final Logger log = LoggerFactory.getLogger(EhCache.class); private org.springframework.cache.Cache cache; public EhCache(org.springframework.cache.Cache cache) { if (cache == null) { throw new IllegalArgumentException("Cache argument cannot be null."); } this.cache = cache; } @Override public V get(K key) throws CacheException { try { if (log.isTraceEnabled()) { log.trace("Getting object from cache [" + cache.getName() + "] for key [" + key + "]"); } if (key == null) { return null; } else { ValueWrapper vw = cache.get(key); if (vw == null) { if (log.isTraceEnabled()) { log.trace("ValueWrapper for [" + key + "] is null."); } return null; } else { return (V) vw.get(); } } } catch (Throwable t) { throw new CacheException(t); } } @Override public V put(K key, V value) throws CacheException { if (log.isTraceEnabled()) { log.trace("Putting object in cache [" + cache.getName() + "] for key [" + key + "]"); } try { V previous = get(key); cache.put(key, value); return previous; } catch (Throwable t) { throw new CacheException(t); } } @Override public V remove(K key) throws CacheException { if (log.isTraceEnabled()) { log.trace("Removing object from cache [" + cache.getName() + "] for key [" + key + "]"); } try { V previous = get(key); cache.evict(key); return previous; } catch (Throwable t) { throw new CacheException(t); } } @Override public void clear() throws CacheException { if (log.isTraceEnabled()) { log.trace("Clearing all objects from cache [" + cache.getName() + "]"); } try { cache.clear(); } catch (Throwable t) { throw new CacheException(t); } } @Override public int size() { try { return ((Ehcache) cache.getNativeCache()).getSize(); } catch (Throwable t) { throw new CacheException(t); } } @Override public Set<K> keys() { try { Ehcache ehcache = (Ehcache) cache.getNativeCache(); @SuppressWarnings("unchecked") List<K> keys = ehcache.getKeys(); if (!CollectionUtils.isEmpty(keys)) { return Collections.unmodifiableSet(new LinkedHashSet<K>(keys)); } else { return Collections.emptySet(); } } catch (Throwable t) { throw new CacheException(t); } } @Override public Collection<V> values() { try { Ehcache ehcache = (Ehcache) cache.getNativeCache(); @SuppressWarnings("unchecked") List<K> keys = ehcache.getKeys(); if (!CollectionUtils.isEmpty(keys)) { List<V> values = new ArrayList<V>(keys.size()); for (K key : keys) { V value = get(key); if (value != null) { values.add(value); } } return Collections.unmodifiableList(values); } else { return Collections.emptyList(); } } catch (Throwable t) { throw new CacheException(t); } } public long getMemoryUsage() { try { return ((Ehcache) cache.getNativeCache()).calculateInMemorySize(); } catch (Throwable t) { return -1; } } public long getMemoryStoreSize() { try { return ((Ehcache) cache.getNativeCache()).getMemoryStoreSize(); } catch (Throwable t) { throw new CacheException(t); } } public long getDiskStoreSize() { try { return ((Ehcache) cache.getNativeCache()).getDiskStoreSize(); } catch (Throwable t) { throw new CacheException(t); } } public String toString() { return "EhCache [" + cache.getName() + "]"; } } |
然后再在spring配置文件中加入shiro的spring配置信息,可以参考之前的博客或者官方的spring联合部分
注意这里shiro部分缓存cacheManage使用上面Spring的cacheManager,而注入到shiro缓存中的类要使用上面的org.apache.shiro.cache.ehcacheSpring3_1.EhCacheManager
改造这样就应该可以了,暂时未验证!
发表评论
-
ehcache 缓存监听器的使用CacheEventListener
2012-09-24 14:33 7624ehcache通过配置,可以实现诸如,内容最长缓存时间,内容最 ... -
spring 3.1中的cache小结
2012-09-24 11:58 3954spring 3.1中有cache了,下面结合目前网上的一些资 ... -
spring Failed to convert property value of type [$Proxy10] to required type
2012-09-12 21:42 4784org.springframework.beans.facto ... -
关于Spring事务代理类型转换问题($ProxyXX cannot be cast to 类型)(
2012-09-12 21:38 4232二、解决方案 后来在网上搜了一下,这样的问题很多 ... -
Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
2009-09-27 00:50 4996实现一个控制器ShirdrnController,如下所示: ... -
ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的路径设置祥解
2009-09-19 10:24 3639package com.openv.spring; ... -
ClassPathXmlApplicationContext 和FileSystemXmlAppli
2009-06-24 12:45 1374ClassPathXmlApplicationContext ... -
PropertyPlaceholderConfigurer
2008-12-03 10:38 2568Spring的框架中为您提供了一个 BeanFactoryPo ... -
Spring MVC 入门 实例
2008-07-03 10:55 5681下面开始开发Spring MVC应用程序 (2)修改WEB- ... -
Spring MVC 配置
2008-07-03 09:38 2062一,配置分发器 DispatcherServlet 是Spri ... -
JdbcTemplate - 查询
2008-06-20 10:40 6794使用JdbcTemplate进行查询时,可以使用queryF ... -
转载]spring+hibernate架构中Dao访问数据库的几种方法
2008-06-20 10:28 7686spring+hibernate架构中Dao访问数据库的几种方 ... -
JdbcTemplate总结
2008-06-20 10:10 26501.您可以使用JdbcTemplate的execute()方法 ... -
//插入很多书(批量插入用法)jdbcTemplate
2008-06-20 10:09 6866//插入很多书(批量插入用法) public void in ... -
JdbcTemplate学习笔记
2008-06-20 09:16 19331、使用JdbcTemplate的execute()方法执行S ...
相关推荐
shiro的权限注解,必须和aop框架整合使用,本资源是springAOP整合shiro的例子。
spring springmvc mybatis shiro 以及 ehcache(配合shiro实现缓存验证 配合spring实现二级缓存) 测试 二级缓存 访问http://localhost:8080/vkblog/test/ehcacheuserlist.action 测试 访问限制 访问任意的action
SpringBoot 、Shiro、 自定义注解权限控制
手把手教你集成spring cloud + shiro微服务框架;用最少的工作量,改造基于shiro安全框架的微服务项目,实现spring cloud + shiro 框架集成。博客地址:...
缓存可以提高查询数据性能, 对同一批数据进行多次查询时, 第一次查询走数据库, 查询数据后,将数据保存在内存中,第二次以后查询 可以直接从内存获取数据,而不需要 和数据库进行交互。
基于springboot微服务框架的个人博客系统,技术栈SpringCloud+MyBatis+Redis+shiro+vue 基于springboot微服务框架的个人博客系统,技术栈SpringCloud+MyBatis+Redis+shiro+vue 基于springboot微服务框架的个人博客...
1,shiro+hibernate4+spring3+easyui+fusioncharts部分bootstrap样式全注解零配置简单权限管理web项目 2,简单shiro权限管控 细度到按钮 3,安装方法按config.properties参数配置你的数据库 4,运行db.sql 5,把项目导入...
使用了Spring MVC,mybaits等技术.. 数据库设计 : User : name--password Role : id--userid--roleName Function : id--userid--url tinys普通用户只能访问index.jsp admin用户通过添加了admin的permission,所以...
spring+mybatis+shiro整合。。。简单入门案例,maven项目
spring mvc、mybatis、bootstrap、shiro 框架整合开发网上求职招聘毕业设计系统、带mysql数据库
Spring+SpringMVC+SpringData+JPA+hibernate+shiro完美整合,你需要的包都在这了
1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,...
Spring Shiro 学习系统 Spring-Shiro-training,适合初学者。
在工作中无处不在的二级缓存,Hibernate -- Shiro -- MyBatis 这三个框架都有二级缓存的技术,总结出详细的配置文档。详解了每步的配置!
● 实现方法: 使用MySQL 建立数据库。服务端使用SSM框架(Spring+SpringMVC+MyBatis)+shiro+Maven,使用Myeclipse进行开发。前端使用MUi和HUI框架和vue(与后台交互模板)和Html5+css3来实现移动端App的开发。
适用于spring集成的javaweb环境
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
开发网上求职招聘毕业设计系统 spring mvc+mybatis+bootstrap+shiro 框架整合、带mysql数据库;有任何不懂的技术问题都可以联系我
spring mvc、mybatis、bootstrap、shiro 框架整合开发网上求职招聘毕业设计系统、带mysql数据库.zip
本项目是基于Spring、Spring MVC、MiniJdbc和Shiro开发的权限管理系统LarvaFrame设计源码,包含436个文件,其中包括145个Java源文件,92个JavaScript文件,30个PNG图片,30个CSS样式文件,27个GIF图片,24个XML文件...