1.导入aspectjwearer坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>FSpring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
默认导入contex坐标就有aop坐标
2.定义dao接口与实现类(过程省略...)
3.定义通知类
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(* com.yulong.Service.*ServiceImpl.*(..))")
private void pt(){};
@Around("pt()")
public void aroung(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
pjp.proceed();
}
long end = System.currentTimeMillis();
long time = end - start;
System.out.println("业务层执行万次用了时间:"+time+"ms");
}
}
注释:@Pointcut("execution(* com.yulong.Service.*ServiceImpl.*(..))":定义要执行bean的方案
@Around("pt()"):环绕执行 pjp.proceed();前后为要执行的内容
4.在通知类上方添加@compoment定义bean,@Aspect通知为Aop
5.在config类上方添加@EnableAspectJAutoProxy开启对Aop注解驱动类支持