日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了&內(nèi)的Spring AOP不能與方法一起使用的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

下面是我的自定義批注。

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value = TransactionalCode.MANAGER, readOnly = true)
public @interface FinanceReadTx {
}

我想對(duì)"MyAnnotation"做些什么,所以我聲明了@Around和如下所示的方法。

@Aspect
@Component
public class TransactionalInterceptor implements Ordered {
    @Around("within(@org.springframework.transaction.annotation.Transactional *) || " +
            "within(@(@org.springframework.transaction.annotation.Transactional *) *)")
    public Object proceed(ProceedingJoinPoint pjp) throws Throwable {
        try {
            setDbType(pjp);
            Object result = pjp.proceed();
            DataSourceContextHolder.clearDataSourceType();
            return result;
        } finally {
            // restore state
            DataSourceContextHolder.clearDataSourceType();
        }
    }
....
}

以下服務(wù)由其他類別自動(dòng)提供(&Q;)。所以我認(rèn)為這不是AOP代理的問題。

@Service
public class UnconfirmedReportService {
    private static final int PREVIEW_SIZE = 8;
    @Autowired
    private UnconfirmedReportRepository unconfirmedReportRepository;
...
    @FinanceHikariReadTx
    public List<UnconfirmedExcelDownloadView> getExcelData(UnconfirmedSearchCondition condition) {
        List<UnconfirmedExcelDownloadView> excelData = newArrayList();
        excelData.addAll(newArrayList(getPurchaseReportDetailExcel(condition)));
        return excelData;
    }
...
}

下面的代碼調(diào)用上述服務(wù)

@Slf4j
@Component
public class UnconfirmedDashboardDetailExcelReader extends SellerExcelReaderTemplate<UnconfirmedExcelDownloadView, UnconfirmedSearchCondition> {
    @Autowired
    private UnconfirmedReportService unconfirmedReportservice;

    @Override public List<UnconfirmedExcelDownloadView> read(String conditionJson) {
        UnconfirmedSearchCondition condition = transformCondition(conditionJson);
        List<UnconfirmedExcelDownloadView> viewList = unconfirmedReportservice.getExcelData(condition);
        return viewList;
    }
...
}

如果@MyAnnotation被注釋為一個(gè)類,則調(diào)用Continue(),但如果方法帶有上述代碼的注釋,則它不起作用。我希望它只使用方法。

我嘗試解決此問題的方法是什么?

推薦答案

您當(dāng)前正在執(zhí)行的操作與我在this answer中解釋的操作類似,即匹配類的(元)批注。

現(xiàn)在您想知道為什么它不匹配方法。我解釋了here。基本上,@within()匹配帶注釋的類中的任何內(nèi)容,而@annotation()匹配帶注釋的方法。問題是,@annotation()需要一個(gè)確切的類型名稱。

但是有另一種方法可以直接在execution()簽名中表示帶注釋的方法。在這里,您還可以選擇以與對(duì)帶注釋的類使用元注釋類似的方式指定元注釋。讓我們將兩者進(jìn)行比較:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MetaAnnotationInterceptor {
  @Before(
    "execution(* *(..)) && (" +
      "within(@de.scrum_master.app.MetaAnnotation *) || " +
      "within(@(@de.scrum_master.app.MetaAnnotation *) *) || " +
      "within(@(@(@de.scrum_master.app.MetaAnnotation *) *) *)" +
    ")"
  )
  public void annotatedClasses(JoinPoint thisJoinPoint){
    System.out.println(thisJoinPoint);
  }

  @Before(
    "execution(@de.scrum_master.app.MetaAnnotation * *(..)) || " +
    "execution(@(@de.scrum_master.app.MetaAnnotation *) * *(..)) || " +
    "execution(@(@(@de.scrum_master.app.MetaAnnotation *) *) * *(..)) "
  )
  public void annotatedMethods(JoinPoint thisJoinPoint){
    System.out.println(thisJoinPoint);
  }
}

后者就是您要找的。只需將de.scrum_master.app.MetaAnnotation替換為org.springframework.transaction.annotation.Transactional,它應(yīng)該適用于您的用例。請(qǐng)確保不要打亂()@*的編號(hào)和嵌套順序,否則很快就會(huì)出現(xiàn)切入點(diǎn)語(yǔ)法錯(cuò)誤。

如果您希望使用一個(gè)或兩個(gè)通知方法,您可以創(chuàng)建一個(gè)包含兩個(gè)切入點(diǎn)的大而雜亂的字符串,或者定義兩個(gè)單獨(dú)的@Pointcut并將它們組合在通知中,用||將它們鏈接起來。

這篇關(guān)于&內(nèi)的Spring AOP不能與方法一起使用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:amp AOP Spring 方法 能與
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定