SpringAop叫做面向切面编程,是一种编程思想。它的作用是在不改动源代码的作用下,对方法进行增强。
增强:在原方法的作用基础上可以添加其他的功能。
几个概念
目标类:要对其方法进行增强的类
切入点:要增强的方法
连接点:目标类中的所有方法,其中连接点的范围大于切入点,因为目标类中的方法可能不是每一个都需要增强
切面:描述切入点与通知建立的对应关系
通知:切入点前后要执行的操作,增强的共性功能
通知类:通知方法所在的类
切入点表达式:要增强的方法的描述方式
通知类型
前置通知:表示当前通知方法在原始切入点方法前运行
后置通知:表示当前通知方法在原始切入点方法后运行
返回后通知:表示当前通知方法在原始切入点方法返回后运行
抛出异常后通知:表示当前通知方法在原始切入点方法抛出异常后运行
环绕通知:表示当前通知方法在原始切入点方法前后运行(比较万能,可以在通知前后任意时段运行)
切入点表达式(execution)的通配符:用来描述切入点,快速描述,并且可以匹配多个方法
*:单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现
匹配org.example包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法
execution(public * org.example.*.UserService.find*(*))
..:多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写
匹配org包下的任意包中的UserService类或接口中所有名称为findById的方法
execution(public User org..UserService.findById(..))
+:专用于匹配子类类型
execution(* *..*Service+.*(..))
xml入门步骤:
编写目标类和通知类(后续还需要在xml文件中设置才真正是通知类,不然就是普通的java类)
目标类
public class User {
//连接点,也是切入点
public void eat() {
System.out.println("吃饭");
//人为设置的异常,以测试抛出异常后的通知
// int a = 1 / 0;
}
//连接点
public void sleep() {
System.out.println("睡觉");
}
}
通知类
public class Advice {
public Object around(ProceedingJoinPoint pjb) {
try {
//前置通知
System.out.println(1);
//原样调用
Object proceed = pjb.proceed();
//返回后通知
System.out.println(2);
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
//抛出异常后通知
System.out.println(3);
} finally {
//后置调用
wash();
}
return null;
}
public void wash() {
System.out.println("洗手");
}
}
}
2.编写applicationContext.xml(名称任意)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置bean-->
<bean id="user" class="org.example.aop.User"/>
<bean id="advice" class="org.example.aop.Advice"/>
<!--aop的aop-->
<aop:config>
<!--设置切面,expression为切面表达式,里面的路径写到要增强的方法-->
<aop:pointcut id="pc" expression="execution(public void org.example.aop.*.eat())"/>
<!--将advice类设置为通知类-->
<aop:aspect ref="advice">
<!--前置通知,可以在切入点之前进行增强-->
<!-- <aop:before method="wash" pointcut-ref="pc"/>-->
<!--设置环绕通知,还需要在通知类中进行控制增强-->
<aop:around method="around" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>
编写测试类
public class App {
public static void main(String[] args) {
//创建一个新的 ClassPathXmlApplicationContext,从给定的 XML 文件加载定义并自动刷新上下文,获取IOC容器
ApplicationContext context1 = new ClassPathXmlApplicationContext("applicationContext.xml");
//按类型获取bean
User user = context1.getBean(User.class);
user.eat();
}
}
运行结果
注解入门步骤(不再需要xml配置文件了)
编写Spring主配置类
//设置当前类为主配置类
@Configuration
//组件扫描,在当前设置的目录路径下扫描
@ComponentScan("org.example")
@EnableAspectJAutoProxy//开启AOP自动代理
public class SpringConfig {
}
编写目标类和通知类(后续还需要再xml文件中设置才是通知类)
目标类
@Component
public class User {
//连接点,也是切入点
public void eat() {
System.out.println("吃饭");
//人为设置的异常,以测试抛出异常后的通知
// int a = 1 / 0;
}
//连接点
public void sleep() {
System.out.println("睡觉");
}
}
通知类
//设置切面
@Aspect
@Component
public class Advice {
//设置切入点,切入点为User中的eat方法
@Pointcut("execution(void org.example.aop.*.eat())")
public void pc() {
}
//第一种方式设置通知来进行方法增强
/*@Before("pc()")
public void before() {
System.out.println(1);
}
@AfterReturning("pc()")
public void afterReturning() {
System.out.println(2);
}
@AfterThrowing("pc()")
public void afterThrowing() {
System.out.println(3);
}
@After("pc()")
public void after() {
System.out.println(4);
}*/
//第二种方式全局配置
@Around("pc()")
public Object around(ProceedingJoinPoint pjb) {
try {
//前置通知
System.out.println(1);
//原样调用
Object proceed = pjb.proceed();
//返回后通知
System.out.println(2);
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
//抛出异常后通知
System.out.println(3);
} finally {
//后置调用
wash();
}
return null;
}
public void wash() {
System.out.println("洗手");
}
}
编写测试类
public class App {
public static void main(String[] args) {
//创建一个新的 AnnotationConfigApplicationContext,从给定的组件类派生 Bean 定义并自动刷新上下文。
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
User user = app.getBean(User.class);
user.eat();
}
}
运行结果