目录
编辑
2.1 需求分析
2.2 思路分析
创建一个Maven项目
导入pom.xml
添加BookDao和BookDaoImpl类
创建Spring的配置类
定义通知类和通知MyAdvice
编写App运行类
运行结果

- 环绕通知必须依赖形参ProceedingJoinPoint才能实现对原始方法的调用,进而实现原始方法调用前后同时添加通知
- 通知中如果未使用ProceedingJoinPoint对原始方法进行调用将跳过原始方法的执行
- 对原始方法的调用可以不接收返回值,通知方法设置成void即可,如果接收返回值,最好设定为Object类型
- 原始方法的返回值如果是void类型,通知方法的返回值类型可以设置成void,也可以设置成Object
- 由于无法预知原始方法运行后是否会抛出异常,因此环绕通知方法必须要处理Throwable异常
介绍完这么多种通知类型,具体该选哪一种呢?
我们可以通过一些案例加深下对通知类型的学习。
环绕通知写法
@Around("pt2()")
public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before advice ...");
//表示对原始操作的调用
Object ret = pjp.proceed();
System.out.println("around after advice ...");
return ret;
}2.1 需求分析
案例设定:测算接口执行效率,但是这个案例稍微复杂了点,我们对其进行简化。
简化设定:在方法执行前输出当前系统时间。
对于SpringAOP的开发有两种方式,XML 和 ==注解==,我们使用哪个呢?
因为现在注解使用的比较多,所以本次课程就采用注解完成AOP的开发。
总结需求为:使用SpringAOP的注解方式完成在方法执行的前打印出当前系统时间。
2.2 思路分析
需求明确后,具体该如何实现,都有哪些步骤,我们先来分析下:
1.导入坐标(pom.xml)
2.制作连接点(原始操作,Dao接口与实现类)
3.制作共性功能(通知类与通知)
4.定义切入点
5.绑定切入点与通知关系(切面)
创建一个Maven项目

导入pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:///POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringAop</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>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>添加BookDao和BookDaoImpl类
package com.itheima.dao;
public interface book {
public void save();
public void update();
}
package com.itheima.dao.impl;
import com.itheima.dao.book;
import org.springframework.stereotype.Repository;
@Repository
public class bookimpl implements book {
@Override
public void save() {
System.out.println("方法1");
}
@Override
public void update() {
System.out.println("方法2");
}
@Override
public int select() {
System.out.println("save()方法执行成功!");
return 100;
}
}创建Spring的配置类

package com.itheima.SpringConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@Component
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}定义通知类和通知MyAdvice


package com.itheima.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class bookaop {
//绑定连接点
@Pointcut("execution(int *com.itheima.*.*book.select(..))")
private void aa(){};
@Pointcut("execution(void *com.itheima.*.*book.update(..))")
private void bb(){};
//绑定切面
@Before("bb()")
//创建通知
public void kk(){
System.out.println("郭浩康加油");
}
//@Before("aa()")
// public void before() {
// System.out.println("before advice ...");
// }
//@After("aa()")
// public void after() {
// System.out.println("after advice ...");
// }
@Around("aa()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before advice ...");
Object proceed = pjp.proceed();
System.out.println("around after advice ...");
return proceed;
}
public void afterReturning() {
System.out.println("afterReturning advice ...");
}
public void afterThrowing() {
System.out.println("afterThrowing advice ...");
}
}编写App运行类
package com.itheima;
import com.itheima.SpringConfig.SpringConfig;
import com.itheima.dao.book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class APP {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
book book = ctx.getBean(book.class);
book.save();
}
}package com.itheima;
import com.itheima.SpringConfig.SpringConfig;
import com.itheima.dao.book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class test {
@Autowired
private book bk;
@Test
public void testsave(){
bk.update();
}
@Test
public void testselect(){
int select = bk.select();
System.out.println(select);
}
}运行结果

















