AOP(面向切面編程),做爲OOP(面向對象編程)的補充,用於處理哪些業務無關的,例如鑑權,日誌等公共邏輯,將之抽取封裝成一個可重用的模塊(切面),減小代碼重複,下降耦合,提升系統可維護性。java
在編譯階段將AspectJ(切面)織入到Java字節碼生成AOP代理類spring
在運行階段在內存中臨時生產一個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("");
}
}
}
複製代碼
execution(* com.virgo.user.service...(..))bash