64,320
社区成员
发帖
与我相关
我的任务
分享https://blog.csdn.net/qq_43585922/article/details/121409070
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
$\textcolor{orange}{AOP的主要功能}$
Spring AOP切面简单理解就是一把刀,在执行过程中可以随意地插入或拔出。

提供声明式事务;允许用户自定义切面

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice

【重点】使用AOP织入,需要导入一个依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
UserService
package com.hxl.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
UserServiceImpl
package com.hxl.service;
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 select() {
System.out.println("查询了一个用户");
}
}
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.hxl.service.UserServiceImpl"/>
<bean id="log" class="com.hxl.log.Log"/>
<bean id="afterLog" class="com.hxl.log.LogAfter"/>
<!--使用原生的Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点;expression:表达式,execution(要执行的位置,修饰词,返回值,类名,方法名,参数)-->
<aop:pointcut id="pointCut" expression="execution(* com.hxl.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointCut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointCut"/>
</aop:config>
</beans>
测试:
import com.hxl.service.UserService;
import com.hxl.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//注意点:动态代理代理的是接口
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
DiyPointCut
package com.hxl.diy;
public class DiyPointCut {
public void before(){
System.out.println("====方法执行前====");
}
public void after(){
System.out.println("====方法执行后====");
}
}
<!--方式二:自定义类-->
<bean id="diy" class="com.hxl.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.hxl.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
package com.hxl.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//方式三:使用注解方式实现aop
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.hxl.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("====方法执行前====");
}
@After("execution(* com.hxl.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行后====");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.hxl.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("====环绕前====");
Signature signature = jp.getSignature();//获得签名
System.out.println("signature:" + signature);
//执行方法
Object proceed = jp.proceed();
System.out.println("=====环绕后====");
}
}
<!--方式三:-->
<bean id="annotationPointCut" class="com.hxl.diy.AnnotationPointCut"/>
<!--开启注解支持
JDK(默认proxy-target-class="false")
cglib(proxy-target-class="true")
-->
<aop:aspectj-autoproxy />