一、定义
AOP:面向切面编程。简单的说AOP就是把我们程序中重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对已有的方法进行增强。
二、AOP相关术语,了解
Joinpoint(连接点):连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。
**pointcut(接入点):**切入点是指我们要对哪些Joinpoint进行拦截的定义。
**Advice(通知/增强):**通知是指拦截到Joinpoint之后所要做的事情。
通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知
**Introduction(引介):**引介是一种特殊的通知。在不修改类代码的前提下,引介可以在运行期为类动态地添加一些方法或者fileld。
**Target(目标对象):**代理的目标对象。
**Weaving(织入):**是指把增强应用到目标对象来创建新的代理对象的过程。
spring采用动态代理织入,二AspectJ采用编译期织入和类装载期织入。
**Proxy(代理):**一个类被AOP织入增强后,就产生一个结果代理类。
**Aspect(切面):**是切入点和通知(引介)的结合
三、xml配置AOP
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="iAccountService" class="com.springlearing.service.impl.AccountServiceImpl"></bean>
<bean id="logger" class="com.springlearing.utils.Logger"></bean>
<!--
切入点表达式的写法:
关键字:execution(表达式)
表达式:访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
标准写法:public void com.springlearing.service.impl.AccountServiceImpl.saveAccount()
省略访问修饰符:void com.springlearing.service.impl.AccountServiceImpl.saveAccount()
返回值用统配符替代:* com.springlearing.service.impl.AccountServiceImpl.saveAccount()
省略包名,用统配符*替代,有几级包就写几个*:* *.*.*.*.AccountServiceImpl.saveAccount()
包名可以使用..表示当前包及其子包:* *..AccountServiceImpl.saveAccount()
类名和方法名都可以使用*来实现统配:* *..*.*()
参数列表:
可以直接写数据类型:
基本类型直接写名称 int
引用类型写包名.类名的方式 java.lang.String
可以使用统配符表示任意类型,但必须有参数
可以使用..表示有无参数均可
全通配写法:* *..*.*(..)
-->
<aop:config>
<aop:aspect id="logAdvice" ref="logger"> <!--增强类的引用 Logger类bean的id-->
<aop:before method="beforeLogger" pointcut="execution(* *..AccountServiceImpl.saveAccount())"></aop:before>
<!--method:要用的在增强类中的方法, pointcut:指定需要被增强的方法是哪些-->
</aop:aspect>
</aop:config>
</beans>
package com.springlearing.service.impl;
import com.springlearing.service.IAccountService;
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("account saved...");
}
}
package com.springlearing.utils;
public class Logger {
public void beforeLogger(){
System.out.println("log print...");
}
}
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!--xml中xmlns:aop用与aop的解析-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
四、实现中问题总结:
//correct way when get bean
IAccountService accountService = (IAccountService) context.getBean("iAccountService");
IAccountService accountService = context.getBean(IAccountService.class);
//error way when get bean
IAccountService accountService = context.getBean(AccountServiceImpl.class);
以上用实现类去拿bean的方式会报错:NoSuchBeanDefinitionException: No qualifying bean of type ‘com.springlearing.service.impl.AccountServiceImpl’ available
原因:要获取的bean中有被切入的方法。该类可能使用反射生成一个新的类
底层是因为byTpe 无法获取bean ,因为它的类型根本就不是 com.springlearing.service.impl.AccountServiceImpl 而是 com.sun.proxy.$Proxy,至于为什么被 切 的 类 在spring 容器中的 type 变了, 那可能要考虑代理反射,可能是 spring 默认使用jdk 动态代理,这种方式会生成一个全新的类