1.什么是AOP?


2.Aop在Spring中作用


1.3 导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式一:使用Spring的接口
接口:
/**
* @author LongXi
* @create 2021-05-30 14:54
*/
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
实现类
/**
* @author LongXi
* @create 2021-05-30 12:18
*/
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
前置log
/**
* @author LongXi
* @create 2021-05-30 14:56
*/
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标
@Override
public void before(Method method, Object[] orgs, Object target) throws Throwable {
System.out.println(target.getClass().getName() +"的"+method.getName()+"被执行了");
}
}
后置log
/**
* @author LongXi
* @create 2021-05-30 14:59
*/
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 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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="userService" class="com.lx.service.UserServiceImpl"/>
<bean id="log" class="com.lx.log.Log"/>
<bean id="afterLog" class="com.lx.log.AfterLog"/>
<!--方式一:使用原生Spring Api接口-->
<!--配置Aop,需要导入AOP的约束-->
<aop:config>
<!--切入点:expression:表达式 execution(要执行的位置!*(修饰词)*(返回值)*(类名)*(方法名)*(参数))-->
<aop:pointcut id="pointcut" expression="execution(* com.lx.service.UserServiceImpl.*(..))"/>
<!--执行后绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试

方式二:自定义类来实现AOP
创建自定义切点类
/**
* @author LongXi
* @create 2021-05-30 16:09
*/
public class DiyPointCut {
public void before(){
System.out.println("****************方法执行前*****************");
}
public void after(){
System.out.println("***************方法执行后************");
}
}
配置类
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="userService" class="com.lx.service.UserServiceImpl"/>
<bean id="log" class="com.lx.log.Log"/>
<bean id="afterLog" class="com.lx.log.AfterLog"/>
<!--方式一:使用原生Spring Api接口-->
<!--配置Aop,需要导入AOP的约束-->
<!-- <aop:config>
<!–切入点:expression:表达式 execution(要执行的位置!*(修饰词)*(返回值)*(类名)*(方法名)*(参数))–>
<aop:pointcut id="pointcut" expression="execution(* com.lx.service.UserServiceImpl.*(..))"/>
<!–执行后绕增加–>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>-->
<!--方式二-->
<bean id="diy" class="com.lx.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,fef要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.lx.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
测试类不变,和方式一 一样
测试出现错误


鼠标左键点过去,看看是不是包引用错了,本例是引用到Demo01里了。
修改之后再测试

实现方式三:使用注解实现!
定义注解类
/**
* @author LongXi
* @create 2021-05-30 16:31
*/
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.lx.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("*****************方法执行前********");
}
@After("execution(* com.lx.service.UserServiceImpl.*(..))")
public void afert(){
System.out.println("*****************方法执行后********");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取切点的位置
@Around("execution(* com.lx.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint pj){
System.out.println("*****************环绕前********");
//执行方法
try {
Object proceed = pj.proceed();
Signature signature = pj.getSignature();
System.out.println("signature:"+ signature);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("*****************环绕后********");
}
}
配置类
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="userService" class="com.lx.service.UserServiceImpl"/>
<bean id="log" class="com.lx.log.Log"/>
<bean id="afterLog" class="com.lx.log.AfterLog"/>
<!--方式一:使用原生Spring Api接口-->
<!--配置Aop,需要导入AOP的约束-->
<!-- <aop:config>
<!–切入点:expression:表达式 execution(要执行的位置!*(修饰词)*(返回值)*(类名)*(方法名)*(参数))–>
<aop:pointcut id="pointcut" expression="execution(* com.lx.service.UserServiceImpl.*(..))"/>
<!–执行后绕增加–>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>-->
<!--方式二-->
<!-- <bean id="diy" class="com.lx.diy.DiyPointCut"/>
<aop:config>
<!–自定义切面,fef要引用的类–>
<aop:aspect ref="diy">
<!–切入点–>
<aop:pointcut id="point" expression="execution(* com.lx.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>-->
<!--方式三-->
<bean id="annotationPointcut" class="com.lx.diy.AnnotationPointCut"/>
<!--开启注解支持 JDK(默认) proxy-target-class="false", cjlib proxy-target-class="true"-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
</beans>
测试类不变
测试结果


6449

被折叠的 条评论
为什么被折叠?



