国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot使用AOP

這篇具有很好參考價值的文章主要介紹了SpringBoot使用AOP。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

Spring相信大家都學(xué)過,就不多述了。

自定義注解,注解的類中所有的接口都會執(zhí)行AOP增強(qiáng),注解的接口會執(zhí)行AOP增強(qiáng)。

注解類:

package xin.students.examManagement.annotation.springConfiguration;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
 * 目標(biāo)方法上添加@UserIfLogin注解,可以在AOP切面中進(jìn)行攔截,并執(zhí)行相應(yīng)的登錄驗證邏輯
 * 這個類本身并不會直接執(zhí)行任何登錄驗證邏輯,它只是作為一個標(biāo)記,用于告訴AOP切面在哪些方法上應(yīng)該執(zhí)行登錄驗證
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresLogin {

}

AOP增強(qiáng)類:

package xin.students.examManagement.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


/**
 * 驗證是否登錄的AOP
 */
@Aspect
@Component
public class IfLoginAspect   {

//這個切點(diǎn)表達(dá)式的作用是確定需要攔截和執(zhí)行切面邏輯的方法,包括添加了@RequiresLogin注解的方法以及添加了@RequiresLogin注解的類的所有方法。        
@Before("@annotation(xin.students.examManagement.annotation.springConfiguration.RequiresLogin) || @within(xin.students.examManagement.annotation.springConfiguration.RequiresLogin)")
    public void validateLogin(JoinPoint joinPoint) {
        System.out.println("開始登錄驗證");
    }
}

SpringBoot主類:

@SpringBootApplication
@EnableAspectJAutoProxy  //開始AOP配置
@ComponentScan(basePackages = "xin.students.examManagement.*")
public class ExamManagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExamManagementApplication.class, args);
    }
}

測試類:

package xin.students.examManagement.configuration;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.students.examManagement.annotation.springConfiguration.RequiresLogin;

@RestController
@RequiresLogin
public class TestController {

    @GetMapping("/secure-page")
    public String securePage() {
        // 這個方法需要登錄驗證
        return "Secure Page";
    }

    @GetMapping("/public-page")
    public String publicPage() {
        // 這個方法不需要登錄驗證
        return "Public Page";
    }
}

添加依賴!

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.7</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.9.7</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.3.13</version>
</dependency>

SpringBoot使用AOP,SSM系列,javaWeb,spring boot,java,spring,AOP

如果你給Service加AOP,給Service所繼承的接口加,不要給Service本身加,要不會報錯的。

如果想改變其返回值,就通過@Around注解。?

    @AfterThrowing(pointcut = "execution(* xin.students.service.TaxExemptionService.*(..))", throwing = "ex")
    public void handleException(Exception ex) {
        System.out.println("出錯了");
    }

    @Around("execution(* xin.students.service.TaxExemptionService.*(..))")
    public Object handleException(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            return joinPoint.proceed(); // 執(zhí)行目標(biāo)方法
        } catch (Exception ex) {
            // 這里可以添加日志記錄異常信息
            return new ResponseEntity<>("no", HttpStatus.BAD_REQUEST);
        }
    }

?文章來源地址http://www.zghlxwxcb.cn/news/detail-528884.html

到了這里,關(guān)于SpringBoot使用AOP的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包