一、切入点基本介绍
1、切面编程名词解释:
-
连接点:类中哪些方法可以被增强,这些方法就叫连接点
-
切入点:实际被真正增强的方法,称为切入点
-
通知(增强)
1)实际增强的逻辑部分称为通知(增强)
2)通知有多种类型:- 前置通知
- 后置通知
- 环绕通知
- 异常通知
- 最终通知 finally
-
切面:是动作,把通知应用到切入点的过程
切面= 切入点+ 额外功能
2、切入点表达式
作用:知道对那个类里面的那个方法进行增强
语法结构: `excution([权限修饰符][返回类型][类全路径][方法名]([参数列表])`
二、AOP基本使用方式
1、AOP操作(AspectJ注解 )
1)、创建 被增强类
//被增强类
public class User {
public void add() {
System.out.println("add 方法执行。。。");
}
}
2)、创建增强类(编写增强逻辑)
在增强类中创建方法,让不同的方法代表不同的通知类型
//增强的类
public class UserProxy {
//前置通知
public void before() {
System.out.println("before ... ");
}
}
3)、进行通知配置
1)在Spring配置文件中,开启注解扫描
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/context/beans/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/aop/beans/spring-aop.xsd">
<!-- 开启注解扫描-->
<context:component-scan base-package="spring.aop"></context:component-scan>
</beans>
2)使用注解创建 User 或 UserProxy 对象
@Component
public class User {
}
------
@Component
//增强的类
public class UserProxy{
}
3)在增强类上添加注解 @Aspect
@Component
@Aspect //生成代理对象
//增强的类
public class UserProxy {
}
4)在Spring的配置文件中开启生成代理对象。
<!--开启aspectJ 生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4)、配置不同类型的通知
1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
@Component
@Aspect //生成代理对象
//增强的类
public class UserProxy {
//前置通知
//before注解表示作为前置通知
@Before(value = "execution(* spring.aop.User.add(..))")
public void before() {
System.out.println("before ... ");
}
//后置通知
@After(value = "execution(* spring.aop.User.add(..))")
public void after() {
System.out.println("after ... ");
}
//后置结果通知
@AfterReturning(value = "execution(* spring.aop.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning ...");
}
//异常通知
@AfterThrowing(value = "execution(* spring.aop.User.add(..))")
public void afterThrowing() {
System.out.println("afterThrowing ... ");
}
//环绕通知
@Around(value = "execution(* spring.aop.User.add(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕之前... ");
//执行被增强的方法
point.proceed();
System.out.println("环绕之后 ... ");
}
}
5)、相同切入点抽取
@Component
@Aspect //生成代理对象
//增强的类
public class UserProxy {
//相同切入点进行抽取:
@Pointcut(value = "execution(* spring.aop.User.add(..))")
public void demo() {
}
//前置通知
//before注解表示作为前置通知
@Before(value = "demo()")
public void before() {
System.out.println("before ... ");
}
}
6)、有多个增强类对同一个方法进行增强,设置增强类的优先级
在增强类上添加注解 @Order(数字类型值)
,数值越小,优先级越高
Component
@Aspect
//order 设置优先级,从0 开始, 数字越小,优先级越高
@Order(1)
public class PersonProxy {
@Before(value = "execution(* spring.aop.User.add(..))")
public void before() {
System.out.println("persion before ");
}
}
测试:
@Test
public void testAspectJ() {
ApplicationContext context = new ClassPathXmlApplicationContext("aopBean.xml");
User user = context.getBean("user", User.class);
user.add();
}
2、AOP操作 (配置文件方式)
1)、创建被增强类 以及增强类:
//被增强类
public class Book {
public void buy() {
System.out.println("buy book ...");
}
//增强类
public class BookProxy {
public void before() {
System.out.println("before buy ... ");
}
}
2)、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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 创建被增强类 及增强类对象 -->
<bean id="book" class="spring.aopxml.Book"></bean>
<bean id="bookProxy" class="spring.aopxml.BookProxy"></bean>
<!--配置aop增强-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="p" expression="execution(* spring.aopxml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!-- 增强作用在具体的方法上-->
<aop:before method="before" pointcut-ref="p"/>
</aop:aspect>
</aop:config>
</beans>
测试:
@Test
public void testAopXml() {
ApplicationContext context = new ClassPathXmlApplicationContext("aopBean2.xml");
Book book = context.getBean("book", Book.class);
book.buy();
}