AOP简介以及Schema-based实现前置后置通知
又双叒叕是程序汪2020-12-24 22:58
AOP简介以及Schema-based实现前置后置通知
一、Aop--面向切面编程 英文(Aspect Oriented Programming)
正常程序都是纵向执行流程如下
而面向切面就意在切
又叫面向切面编程,在原有的纵向程序添加横向程序切面
不需要修改原有程序代码(体现出程序的高扩展性)
原有功能下相当于释放了部分逻辑--让职责更加精确
二、面向切面编程
在原有程序纵向执行流程执行流程中,针对某一些方法添加通知,形成横切面的构成叫做面向切面编程。
常用概念
原有功能:切点,pointcut
前置通知: 在切点之前执行的功能,beforeadvice
后置通知:在切点之后之的功能,afteradvice
如果在切点执行构成中出现异常,会触发异常通知。throws advice
所有的功能的总称叫做切面。
织入:把切面嵌入到原有功能的过程叫做织入。
三、项目演示
第一步、导包
第二、Spring配置文件的 编写
Spring 官方注解帮助文档
搜索Spring xml注解
切面config
有参数切点
无参数切点
三、前置通知
作用:对主程序运行及其代码没有任何影响
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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="mybefore" class="com.wq.advice.MyBeforeAdvice"></bean> <aop:config> <aop:pointcut expression="execution(* com.wq.test.Demo.Deom2())" id="mypoint"/><!-- 切点 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/> </aop:config> <bean id="demo" class="com.wq.test.Demo"></bean> </beans>
测试类
package com.wq.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) { /*Demo demo = new Demo(); demo.Deom1(); demo.Deom2(); demo.Deom3();*/ ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Demo demo = aContext.getBean("demo",Demo.class); demo.Deom1(); demo.Deom2(); demo.Deom3(); }}
结果:
四、后置通知
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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="mybefore" class="com.wq.advice.MyBeforeAdvice"></bean> <bean id="myafter" class="com.wq.advice.MyAfterAdvice"></bean> <aop:config> <aop:pointcut expression="execution(* com.wq.test.Demo.Deom2())" id="mypoint"/><!-- 切点 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/><!-- 形成前置通知 --> <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/><!--形成后置通知 --> </aop:config> <bean id="demo" class="com.wq.test.Demo"></bean> </beans>package com.wq.advice;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class MyAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { // TODO Auto-generated method stub System.out.println("执行后置通知"); }}
实现效果
注意:AOP对程序的高扩展 性
Spring提供了两种AOP的实现方式
1.Schema-base
每个通知都需要实现接口或者类
配置Spring配置文件时在<aop:config>配置
2.Aspectj
每个通知不需要实现接口或类
配置Spring配置文件时是在<aop:config>的子标签<aop:aspect>中配置
Schema-based(模式-基于模式)实现步骤总结
1.导入jar
2.新建通知类(前置,后置)可分别实现
3.Spring的配置文件中配置Spring配置文件
1.引入AOP命名空间
2.配置通知类Bean
3.配置切面
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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="mybefore" class="com.wq.advice.MyBeforeAdvice"></bean> <bean id="myafter" class="com.wq.advice.MyAfterAdvice"></bean> <!-- 配置切面 --> <aop:config> <!-- 配置切点 --> <aop:pointcut expression="execution(* com.wq.test.Demo.Deom2())" id="mypoint"/><!-- 切点 --> <!-- 通知 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/><!-- 形成前置通知 --> <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/><!--形成后置通知 --> </aop:config> <!--配置Demo类 --> <bean id="demo" class="com.wq.test.Demo"></bean> </beans>
4.编写测试代码
5.。运行结果