1、为什么要学习AOP设计原理
- AOP允许我们将通用的流程和代码抽取出来,单独实现,然后给出约定的流程,从而把后续开发者的代码织入约定的流程,从而减少大量重复的工作,使得开发者的工作更为简单,这样业务逻辑就更清晰,代码工作量就更少,尤其是我们核心内容——数据库事务更是如此。
- 是spirng 的一个重点思想。
2、什么是AOP?
- 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
- 主要功能
日志记录,性能统计,安全控制,事务处理,异常处理等等。 - 主要意图
将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。
3、Aop在Spring中的作用
- 提供声明式事务
- 允许用户自定义切面
以下名词需要了解下:
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
4、实现AOP的第一种方式(使用Spring的API接口)
【maven导入AOP依赖包】
<!--springAOP的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
【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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
- service业务层
public interface UserService {
void add();
void delete();
void query();
void update();
}
- 业务层的实现类
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加");
}
@Override
public void delete() {
System.out.println("删除");
}
@Override
public void query() {
System.out.println("查询");
}
@Override
public void update() {
System.out.println("修改");
}
}
3、日志类,在方法前后切入
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
/**
* @param returnValue 方法返回的值
* @param method 要执行的目标的方法
* @param args 参数
* @param target 目标对象
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行" + method.getName() + "返回" + returnValue);
}
}
配置文件
<?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-->
<bean id="userService" class="org.lanqiao.service.UserServiceImpl"/>
<bean id="afterLog" class="org.lanqiao.log.AfterLog"/>
<bean id="beforeLog" class="org.lanqiao.log.BeforeLog"/>
<!--方式一:使用原生Spring API接口-->
<!--配置AOP 需要导入aop的约束-->
<aop:config>
<!--切入点 expression表达式:execution(要执行的位置-修饰词,返回值,类名,方法名,参数)-->
<aop:pointcut id="pointcut" expression="execution(* org.lanqiao.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试类以及结果:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userservice = context.getBean("userService", UserService.class);
userservice.add();
}
}
5、实现AOP的第二种方式(自定义类)
- 创建自定义类
public class Diy {
public void before(){
System.out.println("执行方法前------------------");
}
public void after(){
System.out.println("执行方法后------------------");
}
}
- 配置文件
<!--方式二:自定义类-->
<bean id="diy" class="org.lanqiao.diy.Diy"/>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diy">
<!--切入点 expression表达式:execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* org.lanqiao.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
测试类不变,结果为
实现AOP的第三种方式(注解)
创建一个类,注解实现
//方式三:使用注解方式实现AOP
@Aspect //标注这个类是一个切面
@Component
public class Annotation {
@Before("execution(* org.lanqiao.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("执行方法前------------------");
}
@After("execution(* org.lanqiao.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("执行方法后------------------");
}
@Around("execution(* org.lanqiao.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
Object proceed = joinPoint.proceed();
// Signature signature = joinPoint.getSignature();//获得签名
// System.out.println("signature"+signature);
System.out.println("环绕后");
}
}
配置文件
<!--方式三:-->
<!--<bean id="annotation" class="org.lanqiao.diy.Annotation" />-->
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
<!--扫描包,说明在这个包下的注解会被识别-->
<context:component-scan base-package="org.lanqiao.diy"></context:component-scan>
<!--注解的支持-->
<context:annotation-config></context:annotation-config>
测试结果
总结:AOP的三种实现方式:
- spring原生API实现,创建一个类,实现对应的接口即可。然后在配置文件中配置AOP的切入点和通知(即方法)。当然别忘记了注册bean.
- 自定义类,这个对比于第一个就简单的理解一些,自己定义一个类作为切面,里面的方法作为通知。
- 使用注解实现,其实类比来看和方式二是一样的,只不过是用注解的方式实现了。