spring学习15:AOP
-
AOP:
-
AOP(Aspect Oriented Porgramming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术;
-
-
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分直接的耦合度降低,提高程序的可重用性,同时提高了开发效率;
-
-
AOP在Spring中的作用:
-
提供声明式事务:允许用户自定义切面;
-
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等....
-
切面(ASPECT):横切关注点被模块化的特殊对象,即,它是一个类;
-
通知(Advice):切面必须要完成的工作,即,它是类中的一个方法;
-
目标(Target):被通知对象;
-
代理(Proxy):向目标对象应用通知之后创建的对象;
-
切入点(PointCut):切面通知执行的“地点”的定义;
-
连接点(JoinPoint):与切入点匹配的执行点;
-
-
SpringAOP,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
-
即AOP在不改变原有代码的情况下,去增加新的功能;
通知类型 连接点 实现接口 前置通知 方法前 org.springframework.aop.MethodBeforeAdvice 后置通知 方法后 org.springframework.aop.AfterReturningAdvice 环绕通知 方法前后 org.aopalliance.intercept.MethodInterceptor 异常抛出通知 方法抛出异常 org.springframework.aop.ThrowsAdvice 引介通知 类中增加新的方法属性 org.springframework.aop.IntroductionInterceptor -
-
使用Spring实现AOP:
-
【重点】使用AOP,需要导入一个依赖包;
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency> -
Spring实现AOP的2种实现方式:
-
使用Spring的API接口【主要SpringAPI接口实现】
-
自定义类来实现AOP【主要是切面定义】
-
-
-
方式1:使用Spring的API接口【主要SpringAPI接口实现】
-
execution(修饰符 返回值 包名.类名/接口名.方法名(参数列表))
-
(..)可以代表所有参数
-
()代表一个参数
-
(*,String)代表第一个参数为任何值,第二个参数为String类
-
-
代码案例:UserService接口
//接口
public interface UserService {
void add();
void delete();
void update();
void query();
} -
代码案例:UserServiceImpl
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("修改了一个用户");
}
public void query() {
System.out.println("查询了一个用户");
}
} -
代码案例:applicationContext.xml
-
代码案例:log
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
} -
代码案例:AfterLog
public class AfterLog implements AfterReturningAdvice {
//returnValue:执行后的返回值
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+",返回结果为:"+returnValue);
}
} -
代码案例:测试类
public class MyTest {
-
方式2:自定义类来实现AOP【主要是切面定义】
-
步骤:
-
自定义类:
<bean id="diy" class="com.ljxdemo.diy.DiyPointcut"/>
-
自定义切面:aop:aspect
<aop:config>
<!--自定义切面,ref自定义类-->
<aop:aspect ref="diy">
</aop:aspect>
</aop:config> -
切入点:aop:pointcut
<aop:config>
<!--自定义切面,ref自定义类-->
<aop:aspect ref="diy">
<!--切入点 -->
<aop:pointcut id="point" expression="execution(* xx.xxeImpl.*(..))"/>
</aop:aspect>
</aop:config> -
通知:aop:before
<!--自定义切面,ref自定义类-->
<aop:aspect ref="diy">
<!--切入点 -->
<aop:pointcut id="point" expression="execution(* xx.xxeImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
-
-
-
方式3:注解实现AOP
-
execution(修饰符 返回值 包名.类名/接口名.方法名(参数列表))
-
(..)可以代表所有参数
-
()代表一个参数
-
(*,String)代表第一个参数为任何值,第二个参数为String类
-
-
步骤:
-
自定义类:
<bean id="anno" class="com.ljxdemo.diy.AnnotationPoint"/>
-
类中增加AOP注解:@Aspect ,@Before ,@After, @Around
-
xml配置文件:applicationContext.xml
<!--方式3-->
<bean id="anno" class="com.ljxdemo.diy.AnnotationPoint"/>
<!--开启注解支持
jdk(默认 proxy-target-class="false") :基于接口
cglib (proxy-target-class="true"):基于类
-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
-
-
总结:
-
AOP:在不影响原来业务的情况下,实现业务动态增强!
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律