7.1什么是AOP
意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。(百度百科)
- 主要功能
日志记录,性能统计,安全控制,事务处理,异常处理等等。
- 主要意图
将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。
[图片上传失败...(image-cae4fc-1627269034143)]
7.2AOP在Spring中的作用
提供声明式事务;允许用户自定义切面
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等....
切面(ASPECT)︰横切关注点被模块化的特殊对象。即,它是一个类
通知(Advice) :切面必须要完成的工作。即,它是类中的一个方法。
目标(Target)︰被通知对象。
代理(Proxy) ︰向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知执行的“地点"的定义。
连接点(JointPoint) :与切入点匹配的执行点。
[图片上传失败...(image-487a26-1627269034143)]
SpringAOP中通过Advice定义横切逻辑,Spring中支持5种Advice:
[图片上传失败...(image-38e217-1627269034143)]
即AOP在不改变原有代码的情况下,去增加新的公共的功能
7.3使用Spring实现AOP
方式一:使用SpringAPI来实现(主要是接口实现)
- 接口
public interface UserService {
public void add();
public void del();
public void upa();
public void sel();
}
- 实现类
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加一条数据 ...");
}
@Override
public void del() {
System.out.println("删除一条数据 ...");
}
@Override
public void upa() {
System.out.println("修改一条数据 ...");
}
@Override
public void sel() {
System.out.println("查看数据 ...");
}
}
- 日志类(在执行业务之前的日志类Log和执行业务之后的日志类AfterLog)
public class Log implements MethodBeforeAdvice {
/* Parameters
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 {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果为:"+returnValue);
}
}
- XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--注册bean-->
<bean id="userService" class="com.dong.service.UserServiceImpl"/>
<bean id="log" class="com.dong.log.Log"/>
<bean id="afterLog" class="com.dong.log.AfterLog"/>
<!--配置AOP(需要导入AOP的约束哦)
方式一:使用SpringAPI接口-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="myPointCut" expression="execution(* com.dong.service.UserServiceImpl.*(..))"/>
<!--环绕增强 -->
<aop:advisor advice-ref="log" pointcut-ref="myPointCut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="myPointCut"/>
</aop:config>
</beans>
- 测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//注意这里的返回类型UserService必须是接口,因为动态代理的是接口
UserService userService = (UserService)context.getBean("userService");
userService.add();
}
}
- 结果
com.dong.service.UserServiceImpl的add被执行了
增加一条数据 ...
执行了add返回结果为:null
方式二:自定义类来实现AOP(主要是切面定义)
接口(同上)
实现类(同上)
自定义类(切面类)
public class DiyPointCut {
public void before(){
System.out.println("执行目标方法之前的通知...");
}
public void after(){
System.out.println("执行目标方法之后的通知...");
}
}
XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--注册bean-->
<bean id="userService" class="com.dong.service.UserServiceImpl"/>
<bean id="log" class="com.dong.log.Log"/>
<bean id="afterLog" class="com.dong.log.AfterLog"/>
<!--配置AOP(需要导入AOP的约束哦)
方式二:自定义实现AOP-->
<bean id="diyPointCut" class="com.dong.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diyPointCut">
<!-- 配置切入点-->
<aop:pointcut id="myPointCut" expression="execution(* com.dong.service.UserServiceImpl.*(..))"/>
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="myPointCut" /
<!-- 后置通知 -->
<aop:after-returning method="after" pointcut-ref="myPointCut"/>
<!-- 环绕通知 -->
<!-- <aop:around method="myAround" pointcut-ref="myPointCut" /> -->
<!-- 抛出通知:用于处理程序发生异常-->
<!-- <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e" /> -->
<!-- 最终通知 -->
<!--<aop:after method="myAfter" pointcut-ref="myPointCut" />-->
</aop:aspect>
</aop:config>
</beans>
测试类(同上)
结果
执行目标方法之前的通知...
增加一条数据 ...
执行目标方法之后的通知...
方式三:使用注解实现
接口(同上)
实现类(同上 )
自定义类(切面类)
@Aspect //标识在这个类是一个切面
public class AnnotationPointCut {
// 定义切入点表达式
@Pointcut("execution(* com.dong.service.UserServiceImpl.*(..))")
private void myPointCut(){}// 使用一个返回值为void、方法体为空的方法来命名切入点
// 前置通知
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint) {
System.out.println("前置通知 ...");
System.out.println("目标类是:"+joinPoint.getTarget() );
System.out.println("被织入增强处理的目标方法为:"+joinPoint.getSignature().getName());
}
// 后置通知
/*
@AfterReturning(value="myPointCut()")
public void myAfterReturning(JoinPoint joinPoint) {
System.out.println("后置通知..." );
System.out.println("被织入增强处理的目标方法为:"+joinPoint.getSignature().getName());
}
*/
// 环绕通知
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕开始:执行目标方法之前...");
// 执行当前目标方法
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕结束:执行目标方法之后...");
return obj;
}
// 异常通知
/*
@AfterThrowing(value="myPointCut()",throwing="e")
public void myAfterThrowing(JoinPoint joinPoint, Throwable e) {
System.out.println("异常通知:" + "出错了" + e.getMessage());
}
*/
// 最终通知
/*
@After("myPointCut()")
public void myAfter() {
System.out.println("最终通知:方法结束后...");
}
*/
}
- XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--注册bean-->
<bean id="userService" class="com.dong.service.UserServiceImpl"/>
<bean id="log" class="com.dong.log.Log"/>
<bean id="afterLog" class="com.dong.log.AfterLog"/>
<!--配置AOP(需要导入AOP的约束哦)
方式三:使用注解实现AOP-->
<bean id="annotationPointCut" class="com.dong.diy.AnnotationPointCut"/>
<!--开启注解-->
<aop:aspectj-autoproxy />
</beans>
测试类(同上)
结果
环绕开始:执行目标方法之前...
前置通知 ...
目标类是:com.dong.service.UserServiceImpl@2b6faea6
被织入增强处理的目标方法为:add
增加一条数据 ...
环绕结束:执行目标方法之后...