javascript
pointcut注解_Spring AOP使用指南,详细了解AOP相关注解
Spring AOP 指導(dǎo)教程
什么是Spring AOP
spring aop可以在spring構(gòu)建的系統(tǒng)中使用面向切面編程。當(dāng)然Spring Boot也是基于Spring構(gòu)建的。使用AOP
可以實(shí)現(xiàn)諸如事務(wù),日志以及安全校驗(yàn)等通過切面統(tǒng)一完成的任務(wù)。他可以通過簡單的注解方式實(shí)現(xiàn)在方法執(zhí)行前后來執(zhí)行你自己的邏輯。
什么是advice, joinpoint和pointcut
- Joinpoint:程序的執(zhí)行點(diǎn),如方法的執(zhí)行或者異常的處理。
- Pointcut:一個用來匹配joinpoint的斷言或者表達(dá)式。
- Advice:與一個pointcut關(guān)聯(lián),并在匹配點(diǎn)運(yùn)行。
advices的類型
- Before advice:在join point之前執(zhí)行的advice,不能阻止程序的繼續(xù)運(yùn)行。
- After returning advice:在join point完成之后執(zhí)行的advice。
- After throwing advice:在執(zhí)行的方法拋出異常之后執(zhí)行。
- After advice:在執(zhí)行的join point退出之后執(zhí)行不論正常退出或者拋出了異常。
- Around advice:可以在方法調(diào)用之前或之后執(zhí)行自定義行為,并且還可以選擇繼續(xù)執(zhí)行join point或者執(zhí)行另外的方法。
Spring AOP 示例
編寫Aspectclass然后寫相應(yīng)的執(zhí)行方法并在方法上寫joint-point表達(dá)式
面向切面編程首先需要在類上加@Aspect注解表名這是一個AOP管理的類,然后在方法上加上point-cut表達(dá)式用來匹配joint-point方法。
@Aspectpublic class EmployeeCRUDAspect { @Before("execution(* EmployeeManager.getEmployeeById(..))") //point-cut expression public void logBeforeV1(JoinPoint joinPoint) { System.out.println("EmployeeCRUDAspect.logBeforeV1() : " + joinPoint.getSignature().getName()); }}寫由切面控制的方法(joint points)
首先將類注入到Spring中,之后寫正常的方法即可。
@Componentpublic class EmployeeManager{ public EmployeeDTO getEmployeeById(Integer employeeId) { System.out.println("Method getEmployeeById() called"); return new EmployeeDTO(); }}在上面的例子中,logBeforeV1方法會在getEmployeeById方法執(zhí)行之前執(zhí)行。將會打印如下日志:
EmployeeCRUDAspect.logBeforeV1() : getEmployeeByIdMethod getEmployeeById() calledAOP注解
@Before
在切面控制的方法之前執(zhí)行。
@Aspectpublic class LoggingAspect { @Before("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logBeforeAllMethods(JoinPoint joinPoint) { ... } @Before("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.getEmployeeById(..))") public void logBeforeGetEmployee(JoinPoint joinPoint) { ... }}logBeforeAllMethods方法會在EmployeeManagerImpl中所有方法執(zhí)行前執(zhí)行。
@After
在切面控制的方法之后執(zhí)行。
@Aspectpublic class LoggingAspect { @After("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAfterAllMethods(JoinPoint joinPoint) { ... } @After("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.getEmployeeById(..))") public void logAfterGetEmployee(JoinPoint joinPoint) { ... }}@Around
可以在方法執(zhí)行前后切入。
@Aspectpublic class LoggingAspect { @Around("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAroundAllMethods(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("****LoggingAspect.logAroundAllMethods() : " + joinPoint.getSignature().getName() + ": Before Method Execution"); try { joinPoint.proceed(); } finally { //Do Something useful, If you have } System.out.println("****LoggingAspect.logAroundAllMethods() : " + joinPoint.getSignature().getName() + ": After Method Execution"); }?}@AfterReturning
在方法執(zhí)行完成且沒有拋出任何異常的情況下。
@Aspectpublic class LoggingAspect { @AfterReturning("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAfterReturningAllMethods() throws Throwable { System.out.println("****LoggingAspect.logAfterReturningAllMethods() "); }?}以上示例中只用到了execution方式,還有一種使用@annotation可以對注解了指定接口的方法進(jìn)行切面編程。這種用法在之前的mybatis多數(shù)據(jù)源中使用過。
另外還有很多AOP的注解,如果大家感興趣的話,會繼續(xù)把剩下的用法寫完~
總結(jié)
以上是生活随笔為你收集整理的pointcut注解_Spring AOP使用指南,详细了解AOP相关注解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 富士驱动器ALPHA5手动JOG运行操作
- 下一篇: radio切换控制div显示_JavaS