Spring Cacheソース解析
7386 ワード
このクラスはSpringのキャッシュ・スクリーンショットorg.springframe ebook.cache.interceptor.CacheInterceptorを実現しました.
@SuppressWarnings("serial")
public class CacheInterceptor extends CacheAspectSupport implements MethodInterceptor, Serializable {
private static class ThrowableWrapper extends RuntimeException {
private final Throwable original;
ThrowableWrapper(Throwable original) {
this.original = original;
}
}
//
public Object invoke(final MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
//
Invoker aopAllianceInvoker = new Invoker() {
public Object invoke() {
try {
return invocation.proceed();
} catch (Throwable ex) {
throw new ThrowableWrapper(ex);
}
}
};
try {
// execute
return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
} catch (ThrowableWrapper th) {
throw th.original;
}
}
}
executeの方法を分析します. protected Object execute(Invoker invoker, Object target, Method method, Object[] args) {
//
if (!this.initialized) {
return invoker.invoke();
}
//
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
if (targetClass == null && target != null) {
targetClass = target.getClass();
}
//
final Collection<CacheOperation> cacheOp = getCacheOperationSource().getCacheOperations(method, targetClass);
//
if (!CollectionUtils.isEmpty(cacheOp)) {
Map<String, Collection<CacheOperationContext>> ops = createOperationContext(cacheOp, method, args, target, targetClass);
//
inspectBeforeCacheEvicts(ops.get(EVICT));
// follow up with cacheable
CacheStatus status = inspectCacheables(ops.get(CACHEABLE));
Object retVal = null;
Map<CacheOperationContext, Object> updates = inspectCacheUpdates(ops.get(UPDATE));
if (status != null) {
if (status.updateRequired) {
updates.putAll(status.cUpdates);
}
// return cached object
//
else {
return status.retVal;
}
}
//
retVal = invoker.invoke();
//
inspectAfterCacheEvicts(ops.get(EVICT));
// ,
if (!updates.isEmpty()) {
update(updates, retVal);
}
//
return retVal;
}
// 。
return invoker.invoke();
}