Spring 缓存通知者和切点

缓存切点

  1. /**
  2. * Spring 核心切点抽象
  3. */
  4. public interface Pointcut {
  5. /**
  6. * 类过滤器,当前切点是否需要织入在指定的类上
  7. */
  8. ClassFilter getClassFilter();
  9. /**
  10. * 方法匹配器,当前切点是否需要织入在指定的方法上
  11. */
  12. MethodMatcher getMethodMatcher();
  13. Pointcut TRUE = TruePointcut.INSTANCE;
  14. }
  15. /**
  16. * 检查目标方法是否需要获得通知
  17. */
  18. public interface MethodMatcher {
  19. /**
  20. * 目标方法是否需要获得通知,
  21. * isRuntime() 和此方法返回 false 时,不执行通知。
  22. */
  23. boolean matches(Method method, Class<?> targetClass);
  24. /**
  25. * 是否需要执行运行时匹配【false 表示只需要执行静态匹配即可】
  26. * 如果 isRuntime() 返回 true,则
  27. * matches(Method method, Class<?> targetClass)
  28. * && matches(Method method, Class<?> targetClass, Object... args)
  29. * 都返回 true 时才执行通知
  30. */
  31. boolean isRuntime();
  32. /**
  33. * 当 matches(Method method, Class<?> targetClass) 和 isRuntime() 都返回 true 时,
  34. * 在通知执行前再次进行匹配
  35. */
  36. boolean matches(Method method, Class<?> targetClass, Object... args);
  37. /**
  38. * 匹配所有方法
  39. */
  40. MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;
  41. }
  42. /**
  43. * 不关心运行时参数的静态方法匹配器
  44. */
  45. public abstract class StaticMethodMatcher implements MethodMatcher {
  46. @Override
  47. public final boolean isRuntime() {
  48. return false;
  49. }
  50. @Override
  51. public final boolean matches(Method method, Class<?> targetClass, Object... args) {
  52. // should never be invoked because isRuntime() returns false
  53. throw new UnsupportedOperationException("Illegal MethodMatcher usage");
  54. }
  55. }
  56. /**
  57. * 缓存操作切点抽象
  58. */
  59. @SuppressWarnings("serial")
  60. abstract class CacheOperationSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
  61. /**
  62. * 目标类的指定方法是否需要通知
  63. */
  64. @Override
  65. public boolean matches(Method method, Class<?> targetClass) {
  66. // 排除缓存管理器
  67. if (CacheManager.class.isAssignableFrom(targetClass)) {
  68. return false;
  69. }
  70. final CacheOperationSource cas = getCacheOperationSource();
  71. // 存在缓存操作源 && 指定方法上能解析到缓存操作
  72. return cas != null && !CollectionUtils.isEmpty(cas.getCacheOperations(method, targetClass));
  73. }
  74. @Override
  75. public boolean equals(Object other) {
  76. if (this == other) {
  77. return true;
  78. }
  79. if (!(other instanceof CacheOperationSourcePointcut)) {
  80. return false;
  81. }
  82. final CacheOperationSourcePointcut otherPc = (CacheOperationSourcePointcut) other;
  83. return ObjectUtils.nullSafeEquals(getCacheOperationSource(), otherPc.getCacheOperationSource());
  84. }
  85. @Override
  86. public int hashCode() {
  87. return CacheOperationSourcePointcut.class.hashCode();
  88. }
  89. @Override
  90. public String toString() {
  91. return getClass().getName() + ": " + getCacheOperationSource();
  92. }
  93. /**
  94. * Obtain the underlying {@link CacheOperationSource} (may be {@code null}).
  95. * To be implemented by subclasses.
  96. */
  97. @Nullable
  98. protected abstract CacheOperationSource getCacheOperationSource();
  99. }

缓存通知者

  1. /**
  2. * 持有 AOP 通知的基础接口
  3. */
  4. public interface Advisor {
  5. /**
  6. * 如果没有正确配置通知,则返回一个空通知
  7. * @since 5.0
  8. */
  9. Advice EMPTY_ADVICE = new Advice() {};
  10. /**
  11. * 返回切面的通知
  12. */
  13. Advice getAdvice();
  14. /**
  15. * 此通知是否与具体的实例关联【不可共享】
  16. */
  17. boolean isPerInstance();
  18. }
  19. /**
  20. * 由切入点驱动的通知者接口
  21. */
  22. public interface PointcutAdvisor extends Advisor {
  23. /**
  24. * 获取驱动此 Advisor 的切入点
  25. */
  26. Pointcut getPointcut();
  27. }
  28. @SuppressWarnings("serial")
  29. public abstract class AbstractPointcutAdvisor implements PointcutAdvisor, Ordered, Serializable {
  30. /**
  31. * 此 Advisor 关联切面的顺序值:值越小,越先执行
  32. */
  33. @Nullable
  34. private Integer order;
  35. public void setOrder(int order) {
  36. this.order = order;
  37. }
  38. @Override
  39. public int getOrder() {
  40. if (order != null) {
  41. return order;
  42. }
  43. final Advice advice = getAdvice();
  44. if (advice instanceof Ordered) {
  45. return ((Ordered) advice).getOrder();
  46. }
  47. return Ordered.LOWEST_PRECEDENCE;
  48. }
  49. @Override
  50. public boolean isPerInstance() {
  51. return true;
  52. }
  53. @Override
  54. public boolean equals(Object other) {
  55. if (this == other) {
  56. return true;
  57. }
  58. if (!(other instanceof PointcutAdvisor)) {
  59. return false;
  60. }
  61. final PointcutAdvisor otherAdvisor = (PointcutAdvisor) other;
  62. return ObjectUtils.nullSafeEquals(getAdvice(), otherAdvisor.getAdvice()) &&
  63. ObjectUtils.nullSafeEquals(getPointcut(), otherAdvisor.getPointcut());
  64. }
  65. @Override
  66. public int hashCode() {
  67. return PointcutAdvisor.class.hashCode();
  68. }
  69. }
  70. /**
  71. * 以 BeanFactory 为基础的切点通知者,通知可以配置为 BeanFactory 中的 bean。
  72. */
  73. @SuppressWarnings("serial")
  74. public abstract class AbstractBeanFactoryPointcutAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {
  75. /**
  76. * 通知 Bean 的名称
  77. */
  78. @Nullable
  79. private String adviceBeanName;
  80. /**
  81. * Bean 工厂
  82. */
  83. @Nullable
  84. private BeanFactory beanFactory;
  85. /**
  86. * 延迟初始化的通知对象
  87. */
  88. @Nullable
  89. private transient volatile Advice advice;
  90. /**
  91. * 锁
  92. */
  93. private transient volatile Object adviceMonitor = new Object();
  94. public void setAdviceBeanName(@Nullable String adviceBeanName) {
  95. this.adviceBeanName = adviceBeanName;
  96. }
  97. @Nullable
  98. public String getAdviceBeanName() {
  99. return adviceBeanName;
  100. }
  101. @Override
  102. public void setBeanFactory(BeanFactory beanFactory) {
  103. this.beanFactory = beanFactory;
  104. resetAdviceMonitor();
  105. }
  106. private void resetAdviceMonitor() {
  107. if (beanFactory instanceof ConfigurableBeanFactory) {
  108. adviceMonitor = ((ConfigurableBeanFactory) beanFactory).getSingletonMutex();
  109. }
  110. else {
  111. adviceMonitor = new Object();
  112. }
  113. }
  114. public void setAdvice(Advice advice) {
  115. synchronized (adviceMonitor) {
  116. this.advice = advice;
  117. }
  118. }
  119. /**
  120. * 从 beanFactory 中读取通知实例
  121. */
  122. @Override
  123. public Advice getAdvice() {
  124. Advice advice = this.advice;
  125. // 通知已经初始化,则直接返回
  126. if (advice != null) {
  127. return advice;
  128. }
  129. Assert.state(adviceBeanName != null, "'adviceBeanName' must be specified");
  130. Assert.state(beanFactory != null, "BeanFactory must be set to resolve 'adviceBeanName'");
  131. // 通知 bean 是单例
  132. if (beanFactory.isSingleton(adviceBeanName)) {
  133. // 依赖于 Bean 工厂提供的单例语义
  134. advice = beanFactory.getBean(adviceBeanName, Advice.class);
  135. this.advice = advice;
  136. return advice;
  137. }
  138. else {
  139. synchronized (adviceMonitor) {
  140. advice = this.advice;
  141. if (advice == null) {
  142. advice = beanFactory.getBean(adviceBeanName, Advice.class);
  143. this.advice = advice;
  144. }
  145. return advice;
  146. }
  147. }
  148. }
  149. @Override
  150. public String toString() {
  151. final StringBuilder sb = new StringBuilder(getClass().getName());
  152. sb.append(": advice ");
  153. if (adviceBeanName != null) {
  154. sb.append("bean '").append(adviceBeanName).append("'");
  155. }
  156. else {
  157. sb.append(advice);
  158. }
  159. return sb.toString();
  160. }
  161. }
  162. /**
  163. * 由 CacheOperationSource 驱动的 Advisor
  164. */
  165. @SuppressWarnings("serial")
  166. public class BeanFactoryCacheOperationSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor {
  167. @Nullable
  168. private CacheOperationSource cacheOperationSource;
  169. /**
  170. * 缓存操作切点
  171. */
  172. private final CacheOperationSourcePointcut pointcut = new CacheOperationSourcePointcut() {
  173. @Override
  174. @Nullable
  175. protected CacheOperationSource getCacheOperationSource() {
  176. return cacheOperationSource;
  177. }
  178. };
  179. public void setCacheOperationSource(CacheOperationSource cacheOperationSource) {
  180. this.cacheOperationSource = cacheOperationSource;
  181. }
  182. public void setClassFilter(ClassFilter classFilter) {
  183. pointcut.setClassFilter(classFilter);
  184. }
  185. @Override
  186. public Pointcut getPointcut() {
  187. return pointcut;
  188. }
  189. }

Spring 由缓存切点驱动的通知者的更多相关文章

  1. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  2. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  3. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  4. Spring cache 缓存

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  5. Spring实战——缓存

    缓存 提到缓存,你能想到什么?一级缓存,二级缓存,web缓存,redis-- 你所能想到的各种包罗万象存在的打着缓存旗号存在的各种技术或者实现,无非都是宣扬缓存技术的优势就是快,无需反复查询等. 当然 ...

  6. Spring 对缓存的抽象

    Cache vs. Buffer A buffer is used traditionally as an intermediate temporary store for data between ...

  7. 谈谈spring的缓存

    缓存到底扮演了什么角色 请移步:  http://hacpai.com/article/1376986299174 在对项目进行优化的时候,我们可以主要从以下三个方面入手: 1 缓存 2 集群 3 异 ...

  8. Spring之缓存注解@Cacheable

    https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...

  9. Java缓存学习之五:spring 对缓存的支持

    (注意标题,Spring对缓存的支持 这里不单单指Ehcache ) 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache ...

随机推荐

  1. localStorage 理解

    localStorage对象是HTML5的客户端存储持久化数据的方案.为了能访问到同一个localStorage对象,页面必须来自同一个域名(子域名无效),使用同一种协议,在同一个端口上. 过期策略: ...

  2. jqery实现10X10的表格,双击消失

    <script type="text/javascript"> $(document).ready(function(){ //循环拼接html s="&qu ...

  3. Redis的频道发布与消息订阅

    Redis的频道发布与消息订阅 官网介绍 进程间的一种通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 订阅/发布消息图 下图展示了频道channel1,以及订阅这个频道的三个客户端-c ...

  4. 用 C++ 模板元编程实现有限的静态 introspection

    C++ 中的奇技淫巧大部分来源于模板技术,尤其是模版元编程技术(Template Meta-Programming, TMP).TMP 通过将一部分计算任务放在编译时完成,不仅提高了程序的性能,还能让 ...

  5. 三、Signalr外部链接

    一.本地外部引用 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  6. java.lang.Integer 类(JDK1.7)

    1.Integer 和int 的区别 ①.Integer 是 int 包装类,int 是八大基本数据类型之一(byte,char,short,int,long,float,double,boolean ...

  7. Big Data(五)关于Hadoop的HA的实践搭建

    JoinNode 分布在node01,node02,node03 1.停止之前的集群 2.免密:node01,node02 node02: cd ~/.ssh ssh-keygen -t dsa -P ...

  8. 牛客练习赛26 D xor序列 (线性基)

    链接:https://ac.nowcoder.com/acm/contest/180/D 来源:牛客网 xor序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...

  9. 三、Ubuntu16.04 安装Jira8.2.2(自带中文包)和破解

    一.环境准备 (一)安装java Jira7.2的运行是依赖java环境的,也就是说需要安装jdk并且要是1.8以上版本,如下: java -version没有任何显示需要下载安装 1.下载链接:ht ...

  10. VS2013 删除"附加依赖项"中“继承的值”

    经过好几次尝试,都无法在VS2013中直接删除“继承的值”,于是另辟蹊径,找到了一种解决方法. 相对而言,在 VS2010 中干这件事会容易一点,或者说,成功率更高一点,于是,我的思路就是再装一个 V ...