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>
如果你給Service加AOP,給Service所繼承的接口加,不要給Service本身加,要不會報錯的。
如果想改變其返回值,就通過@Around注解。?文章來源:http://www.zghlxwxcb.cn/news/detail-528884.html
@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)!