概述
1.什么是AOP(面向切面编程)
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
2.AOP的业务逻辑
在不改变代码的情况下,添加新的功能
3.AOP在spring中的作用(基本术语)
- 切面(Aspect):横切关注点 被模块化 的特殊对象。即,它是一个类。
- 目标对象(Target):指将要被增强的对象,即包含主业务逻辑的类对象。
- 连接点(JoinPoint):与切入点匹配的执行点。
- 切入点(PointCut):切面通知 执行的 “地点”的定义。
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
- 织入(Weaving):织入是将切面和业务逻辑对象连接起来, 并创建通知代理的过程。
- 增强器(Adviser):Advisor由切入点和Advice组成。
- 代理(Proxy):向目标对象应用通知之后创建的对象。
4.五种类型的Advice及接口
5.XML中AOP的配置元素
6.注解中的AOP指令
使用AOP编程的三种方式
1.第一种方式:使用Spring的API 接口
(1)导包,添加AOP依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
(2)编写UserSerive接口及实现类
package Serive;
public interface UserSerive {
public void add();
public void delete();
public void update();
public void searth();
}
package Serive;
public class Impl implements UserSerive {
public void add() { System.out.println("增加用户"); }
public void delete() { System.out.println("删除用户"); }
public void update() { }
public void searth() { }
}
(3)编写两个插入类,实现AOP接口
package Serive.Config;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method 要执行的目标方法
//objects:要调用这方法的参数
//o:要调用的目标对象
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"de "+ method.getName()+"");
}
}
package Serive.Config;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfter implements AfterReturningAdvice {
public void afterReturning(Object o1, Method method, Object[] objects, Object o) throws Throwable {//o1:返回值
System.out.println(o.getClass().getName()+"de "+ method.getName()+""+o1);
}
}
(4)在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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="UserSerive" class="Serive.Impl"></bean>
<bean id="Log" class="Serive.Config.Log"></bean>
<bean id="LogAfter" class="Serive.Config.LogAfter"></bean>
<!--espression:类名全称 public com.service.方法-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* Serive.Impl.*(..))"></aop:pointcut>
<aop:advisor advice-ref="Log" pointcut-ref="pt"></aop:advisor>
<aop:advisor advice-ref="LogAfter" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beens>
注意:添加AOP命令空间
(5)测试运行
import Serive.UserSerive;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Text {
@Test
public void Text1(){
ApplicationContext Context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserSerive userSerive = (UserSerive) Context.getBean("UserSerive");
userSerive.add();
}
}
2.第二种方式:自定义来实现AOP
- 比第一种方式简单,只需要编写一个插入类
package Serive.Config;
public class diy {
public void before(){ System.out.println("=前面========"); }
public void after(){ System.out.println("==后面===="); }
}
}
- 在XML中配置AOP
<bean id="UserSerive" class="Serive.Impl"></bean>
<bean id="diy" class="Serive.Config.diy"></bean>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="pt" expression="execution(* Serive.Impl.*(..))"></aop:pointcut>
<aop:before method="before" pointcut-ref="pt"></aop:before>
<aop:after method="after" pointcut-ref="pt"></aop:after>
</aop:aspect>
</aop:config>
注意:添加AOP命令空间
3.第三种方式:使用注解
- 编写一个插入类
package Serive.Config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class diy {
@Before("execution(* Serive.Impl.*(..))")
public void before(){
System.out.println("=前面========");
}
@After("execution(* Serive.Impl.*(..))")
public void after(){
System.out.println("==后面====");
}
@Around("execution(* Serive.Impl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
System.out.println(joinPoint.getSignature());//执行的方法是哪一个
Object proceed = joinPoint.proceed();//执行方法
System.out.println("环绕后");
System.out.println(proceed);
}
}
- 在XML中进行配置
<aop:aspectj-autoproxy/> //自动识别注解,必须加上
<bean id="diy" class="Serive.Config.diy"></bean>
<bean id="UserSerive" class="Serive.Impl"></bean>
注意:添加AOP命令空间
运行结果:
先执行环绕的方法,执行before方法,最后执行after方法