一、什么是AOP
AOP(Aspect Oriented Programming):面向切面编程,通过预编译的方式和运行期动态代理实现程序功能的统一维护的一种技术。
二、使用步骤
在pom中添加一下依赖:
<!-- <parent>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.3.3.RELEASE</version>-->
<!-- </parent>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
方式一:
1.编写切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect {
Logger logger = LoggerFactory.getLogger(MyAspect.class);
@Pointcut("execution(public * com.learn.demo.mylearnaop.service.impl.CalcServiceImpl.*(..))")
public void myPointCut(){
}
@Before("myPointCut()")
public void beforeNotify() {
logger.info("**********@Before前置通知MyAspect");
}
@After("myPointCut()")
public void afterNotify() {
logger.info("**********@Afte后置通知MyAspect");
}
@AfterReturning("myPointCut()")
public void afterReturningNotify() {
logger.info("**********@afterReturning返回后通知MyAspect");
}
@AfterThrowing("myPointCut()")
public void AfterThrowingNotify() {
logger.info("**********@AfterThrowing异常通知MyAspect");
}
@Around("myPointCut()")
public Object AroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object proceed = proceedingJoinPoint.proceed();
logger.info("**********@Around环绕通知MyAspect");
return proceed;
}
}
2.实现类
import com.learn.demo.mylearnaop.service.CalcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class CalcServiceImpl implements CalcService {
private static Logger logger = LoggerFactory.getLogger(CalcServiceImpl.class);
@Override
public void testAop() {
logger.info("=========testAop=============");
}
}
3.单元测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class MylearnaopApplicationTests {
@Resource
private CalcService calcService;
@Test
public void testAop() {
calcService.testAop();
}
}
4.测试结果
方式二:
1.实现自定义注解
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "MyAnnotation注解";
}
2.编写切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect {
Logger logger = LoggerFactory.getLogger(MyAspect.class);
@Pointcut("@annotation(com.learn.demo.mylearnaop.annotation.LogAnnotation)")
// @Pointcut("execution(public * com.learn.demo.mylearnaop.service.impl.CalcServiceImpl.*(..))")
public void myPointCut() {
}
@Before("myPointCut()")
public void beforeNotify() {
logger.info("**********@Before前置通知MyAspect");
}
@After("myPointCut()")
public void afterNotify() {
logger.info("**********@Afte后置通知MyAspect");
}
@AfterReturning("myPointCut()")
public void afterReturningNotify() {
logger.info("**********@afterReturning返回后通知MyAspect");
}
@AfterThrowing("myPointCut()")
public void AfterThrowingNotify() {
logger.info("**********@AfterThrowing异常通知MyAspect");
}
@Around("myPointCut()")
public Object AroundNotify(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object proceed = proceedingJoinPoint.proceed();
logger.info("**********@Around环绕通知MyAspect");
return proceed;
}
}
3.使用注解
import com.learn.demo.mylearnaop.annotation.MyAnnotation;
import com.learn.demo.mylearnaop.service.CalcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class CalcServiceImpl implements CalcService {
private static Logger logger = LoggerFactory.getLogger(CalcServiceImpl.class);
@Override
@MyAnnotation
public void testAop() {
logger.info("=========testAop=============");
}
}
4.单元测试
import com.learn.demo.mylearnaop.service.CalcService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MylearnaopApplicationTests {
@Resource
private CalcService calcService;
@Test
public void testAop() {
calcService.testAop();
}
}
5.测试结果