AOP(面向切面编程),作为OOP(面向对象编程)的补充,用于处理哪些业务无关的,例如鉴权,日志等公共逻辑,将之抽取封装成一个可重用的模块(切面),减少代码重复,降低耦合,提高系统可维护性。
在 编译阶段 将AspectJ(切面)织入到Java字节码生成AOP代理类
在 运行阶段 在内存中临时生产一个AOP对象且在特定的切点做了增强处理
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 复制代码
无 复制代码
无 复制代码
package com.virgo.user.auto; import com.virgo.user.service.TestService; import com.virgo.user.service.TestServiceImpl; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.DeclareParents; import org.springframework.stereotype.Component; /** * @author zhaozha * @date 2019/10/24 下午1:00 */ @Aspect @Component public class IntroductionAop { @DeclareParents(value = "com.virgo.user..service..*", defaultImpl = TestServiceImpl.class) public TestService testService; } package com.virgo.user.service; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * @author zhaozha * @date 2019/10/24 下午1:02 */ @Service @Slf4j public class TestServiceImpl implements TestService{ @Override public void test() { log.info("all can use"); } } ... // CommonService使用TestService TestService testService = (TestService)commonServiceImpl; testService.test(); ... 复制代码
package com.virgo.user.auto; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @author zhaozha * @date 2019/10/24 下午1:29 */ @Slf4j @Aspect @Component @Order(1) public class TestAopOrder1 { @Pointcut("execution(* com.virgo.user.service.*.*(..))") public void pointcut() { } @Before("pointcut()") public void begin() { log.info("2:{}","before"); } @After("pointcut()") public void commit() { log.info("9:{}","after"); } @AfterReturning("pointcut()") public void afterReturning(JoinPoint joinPoint) { log.info("10:{}","afterReturning"); } @AfterThrowing("pointcut()") public void afterThrowing() { log.info("afterThrowing"); } @Around("pointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { try { log.info("1:{}","around"); return joinPoint.proceed(); } catch (Throwable e) { e.printStackTrace(); throw e; } finally { log.info("8:{}","around"); } } } package com.virgo.user.auto; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @author zhaozha * @date 2019/10/24 下午1:11 */ @Slf4j @Aspect @Component @Order(2) public class TestAopOrder2 { @Pointcut("execution(* com.virgo.user.service.*.*(..))") public void pointcut() { } @Before("pointcut()") public void begin() { log.info("4:{}","before"); } @After("pointcut()") public void commit() { log.info("6:{}","after"); } @AfterReturning("pointcut()") public void afterReturning(JoinPoint joinPoint) { log.info("7:{}","afterReturning"); } @AfterThrowing("pointcut()") public void afterThrowing() { log.info("afterThrowing"); } @Around("pointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { try { log.info("3:{}","around"); return joinPoint.proceed(); } catch (Throwable e) { e.printStackTrace(); throw e; } finally { log.info("5:{}","around"); } } } 复制代码
package com.virgo.user.auto; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author zhaozha * @date 2019/10/24 下午1:39 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LogAop { String value() default ""; } package com.virgo.user.auto; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @author zhaozha * @date 2019/10/24 下午1:53 */ @Slf4j @Aspect @Component @Order(1) public class TestAnnotationAop { @Pointcut(value = "@annotation(logAop)", argNames = "logAop") public void pointcut(LogAop logAop) { } @Around(value = "pointcut(logAop)", argNames = "joinPoint,logAop") public Object around(ProceedingJoinPoint joinPoint,LogAop logAop) throws Throwable { try { log.info(logAop.value()); return joinPoint.proceed(); } catch (Throwable e) { e.printStackTrace(); throw e; } finally { log.info(""); } } } 复制代码