AOP实现
声明:本文章属于学习笔记,根据狂神说的Spring编写
Spring官方文档:Spring官方文档
一丶什么是AOP
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
我们举个简单的例子:
我们在看上篇笔记的时候有一个代理模式,我们看到的是可以在我们的增删改查中添加一个日志方法,其实这也可以间接的说明,这个代理模式的实现其实降低了程序的耦合性。

我们看这一张图片假如我们处理一个业务,但是我们在不改变原来业务代码的情况下,我们可以就将这个代理横向切入进去。这也就是Aop的底层,也就是Aop的基本思想。所以说AOP在Java里是利用动态代理实现的。如果简单的将AOP概括成一句话:这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。
二丶Spring原生API实现
我们用代码来感受一下AOP:
我们因为要将方法添加进去某个类的指定方法中,所以我们在不改变业务的前提一定要进行横向切入!
UserService接口:
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
实现类:
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("1");
}
@Override
public void delete() {
System.out.println("1");
}
@Override
public void update() {
System.out.println("1");
}
@Override
public void query() {
System.out.println("1");
}
}
我们只显示写了一个看似不咋规范的增删改查方法,在接口的实现类中,我们要做的就是在这些方法中加入我们的日志方法。
我们会想如果我们用代理模式也是可以的,但是在这里我用spring原生的API实现。
日志方法:
public class Log implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(method.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"返回了结果为:"+o);
}
}
核心配置文件bean:
<?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="userservice" class="com.kdy.service.UserServiceImpl">
</bean>
<bean id="log" class="com.kdy.log.Log">
</bean>
<bean id="afterlog" class="com.kdy.log.AfterLog">
</bean>
<!--方式一:使用原生Spring API接口 -->
<!--配置aop,要导入aop约束-->
<aop:config>
<!--切入点:execution要切入的位置-->
<aop:pointcut id="pointcut" expression="execution(* com.kdy.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor pointcut-ref="pointcut" advice-ref="log"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
首先我们可以看到,AOP切入需要进行两个步骤:
1丶寻找切入点,也就是我们需要在哪里进行切入。
2丶执行环绕增加,也就是说我们要在切入点中切入什么。

测试类测试:
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beanx.xml");
//动态代理,代理的是接口,所以要用接口的类型接收返回值
UserService bean = (UserService) context.getBean("userservice");
bean.add();
}
}

所以AOP在逻辑上简单的来说,就是在类的什么方法中添加什么方法。AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
注:关于execution()方法的讲解:execution详细讲解
三丶自定义切面

因为是自定义类,所以我们要自定义一个类:
public class diy {
public void before(){
System.out.println("1");
}
public void after(){
System.out.println("2");
}
}
<!--方式二:自定义切面-->
<aop:config>
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="puintcut" expression="execution(* com.kdy.service.UserServiceImpl.*(..))" />
<!--通知-->
<aop:before method="before" pointcut-ref="puintcut"/>
<aop:after method="after" pointcut-ref="puintcut"/>
</aop:aspect>
</aop:config>

四丶用注解去实现AOP
根据以往的经验来说如果我们用注解来进行开发那么我们通常是一般不在使用bean的配置文件。
但是这次不同,我们还是需要bean的配置文件进行支持,因为我们创建的对象都是通过bean来创建的。

@Aspect
public class koda {
@Before("execution(* com.kdy.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("我最先开始执行");
}
@After("execution(* com.kdy.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("我之后开始执行");
}
@Around("execution(* com.kdy.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp){
System.out.println("环绕前");
Signature signature = jp.getSignature();
System.out.println(signature);
System.out.println("环绕后");
}
}
测试类的测试和上面的一样,我们直接看运行结果:

6762




被折叠的 条评论
为什么被折叠?



