Spring中的AOP

事物中的aop

PlatformTransactionManager

@Transactional注解中殷绍,PlatformTransactionManager,使用TransactionTemplate或者通過(guò)aop使用聲明式事物。
TransactionInterceptor->invoke->invokeWithinTransaction調(diào)用進(jìn)入TransactionAspectSupport->invokeWithinTransaction.
創(chuàng)建事物->執(zhí)行方法->如果有異常調(diào)用completeTransactionAfterThrowing回滾->最后清理事物信息,恢復(fù)當(dāng)前線程中ThreadLocal事物狀態(tài)->返回事物狀態(tài)TransactionStatus

// Standard transaction demarcation with getTransaction and commit/rollback calls.
TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
Object retVal = null;
try {
    // This is an around advice: Invoke the next interceptor in the chain.
    // This will normally result in a target object being invoked.
    retVal = invocation.proceedWithInvocation();
}
catch (Throwable ex) {
    // target invocation exception
    completeTransactionAfterThrowing(txInfo, ex);
    throw ex;
}
finally {
    cleanupTransactionInfo(txInfo);
}
commitTransactionAfterReturning(txInfo);
spring cache中的aop

在@EnableCaching注解類(lèi)中


加了@Cacheable的方法在執(zhí)行的時(shí)候,織入CacheInterceptor乌叶。
CacheInterceptor->invoke->CacheAspectSupport->execute
先查緩存,沒(méi)查到直接執(zhí)行方法

@Nullable
protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) {
    // Check whether aspect is enabled (to cope with cases where the AJ is pulled in automatically)
    if (this.initialized) {
        Class<?> targetClass = getTargetClass(target);
        CacheOperationSource cacheOperationSource = getCacheOperationSource();
        if (cacheOperationSource != null) {
            Collection<CacheOperation> operations = cacheOperationSource.getCacheOperations(method, targetClass);
            if (!CollectionUtils.isEmpty(operations)) {
                return execute(invoker, method,
                        new CacheOperationContexts(operations, method, args, target, targetClass));
            }
        }
    }

    return invoker.invoke();
}

@Nullable
    private Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) {
        // Special handling of synchronized invocation
        if (contexts.isSynchronized()) {
            CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();
            if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) {
                Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
                Cache cache = context.getCaches().iterator().next();
                try {
                    return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker))));
                }
                catch (Cache.ValueRetrievalException ex) {
                    // The invoker wraps any Throwable in a ThrowableWrapper instance so we
                    // can just make sure that one bubbles up the stack.
                    throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause();
                }
            }
            else {
                // No caching required, only call the underlying method
                return invokeOperation(invoker);
            }
        }


        // Process any early evictions
        processCacheEvicts(contexts.get(CacheEvictOperation.class), true,
                CacheOperationExpressionEvaluator.NO_RESULT);

        // Check if we have a cached item matching the conditions
        Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));

        // Collect puts from any @Cacheable miss, if no cached item is found
        List<CachePutRequest> cachePutRequests = new LinkedList<>();
        if (cacheHit == null) {
            collectPutRequests(contexts.get(CacheableOperation.class),
                    CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);
        }

        Object cacheValue;
        Object returnValue;

        if (cacheHit != null && !hasCachePut(contexts)) {
            // If there are no put requests, just use the cache hit
            cacheValue = cacheHit.get();
            returnValue = wrapCacheValue(method, cacheValue);
        }
        else {
            // Invoke the method if we don't have a cache hit
            returnValue = invokeOperation(invoker);
            cacheValue = unwrapReturnValue(returnValue);
        }

        // Collect any explicit @CachePuts
        collectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests);

        // Process any collected put requests, either from @CachePut or a @Cacheable miss
        for (CachePutRequest cachePutRequest : cachePutRequests) {
            cachePutRequest.apply(cacheValue);
        }

        // Process any late evictions
        processCacheEvicts(contexts.get(CacheEvictOperation.class), false, cacheValue);

        return returnValue;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市伯铣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌轮纫,老刑警劉巖腔寡,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異掌唾,居然都是意外死亡放前,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)糯彬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)凭语,“玉大人,你說(shuō)我怎么就攤上這事撩扒∷迫樱” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)虫几。 經(jīng)常有香客問(wèn)我锤灿,道長(zhǎng),這世上最難降的妖魔是什么辆脸? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任但校,我火速辦了婚禮,結(jié)果婚禮上啡氢,老公的妹妹穿的比我還像新娘状囱。我一直安慰自己,他們只是感情好倘是,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布亭枷。 她就那樣靜靜地躺著,像睡著了一般搀崭。 火紅的嫁衣襯著肌膚如雪叨粘。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,590評(píng)論 1 305
  • 那天瘤睹,我揣著相機(jī)與錄音升敲,去河邊找鬼。 笑死轰传,一個(gè)胖子當(dāng)著我的面吹牛驴党,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播获茬,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼港庄,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了恕曲?” 一聲冷哼從身側(cè)響起鹏氧,我...
    開(kāi)封第一講書(shū)人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎码俩,沒(méi)想到半個(gè)月后度帮,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡稿存,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瞳秽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓣履。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖练俐,靈堂內(nèi)的尸體忽然破棺而出袖迎,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布燕锥,位于F島的核電站辜贵,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏归形。R本人自食惡果不足惜托慨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望暇榴。 院中可真熱鬧厚棵,春花似錦、人聲如沸蔼紧。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)奸例。三九已至彬犯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間查吊,已是汗流浹背躏嚎。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留菩貌,地道東北人卢佣。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像箭阶,于是被迫代替她去往敵國(guó)和親虚茶。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 大家好仇参,我是IT修真院上海分院第02期學(xué)員嘹叫,一枚正直善良的java程序員。今天給分享一下修真院java任務(wù)一中關(guān)于...
    刻礪思閱讀 291評(píng)論 0 0
  • 一诈乒、aop 思想介紹 橫向重復(fù)罩扇,縱向抽取 filter 動(dòng)態(tài)代理 interceptor中 二、spring中...
    林皮皮s閱讀 369評(píng)論 0 0
  • Spring中的AOP【面向切面編程】 就是把我們程序重復(fù)的代碼抽取出來(lái)怕磨,在需要執(zhí)行的時(shí)候喂饥,使用動(dòng)態(tài)代理的技術(shù),在...
    哈哈大圣閱讀 807評(píng)論 0 4
  • AOP(Aspect Oriented Programming肠鲫,面向切面編程)员帮,通過(guò)提供另一種思考程序的方式來(lái)補(bǔ)充...
    yu_liu閱讀 264評(píng)論 0 0
  • AOP的定義 AOP基本上是通過(guò)代理機(jī)制實(shí)現(xiàn)的,他是OOP的延續(xù)也是spring框架中最重要的內(nèi)容之一. 定義:運(yùn)...
    長(zhǎng)生藤閱讀 249評(píng)論 0 0