Spring Study -lesson12 -AOP(一)-2023-03-18
AOP方法一:通过Spring API接口实现
<bean id="userService" class="com.feijian.service.UserServiceImpl"/>
<bean id="log" class="com.feijian.log.Log"/>
<bean id="afterLog" class="com.feijian.log.AfterLog"/>
<!-- 方式一:使用原生spring API接口-->
<!--配置AOP -->
<!-- <aop:config>-->
<!-- <!–切入点 execution要执行的位置–>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.feijian.service.UserServiceImpl.*(..))"/>-->
<!-- -->
<!-- <!–执行环绕增加!–>-->
<!-- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!-- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
<!-- </aop:config>-->
AOP方法二:自定义类实现-主要是切面定义
package com.feijian.diy;
public class DiyPointCut {
public void before(){
System.out.println("--------------方法执行前-----------------");
}
public void after(){
System.out.println("--------------方法执行后-----------------");
}
}
<!-- 方法二:自定义类-->
<bean id="diy" class="com.feijian.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<!-- 切入点-->
<aop:pointcut id="point" expression="execution(* com.feijian.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
第三,使用注解实现AOP
package com.feijian.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;
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.feijian.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("==============方法执行前===================");
}
@After("execution(* com.feijian.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("==============方法执行后===================");
}
@Around("execution(* com.feijian.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
}
}
<!--方式三-->
<bean id="annotationPointCut" class="com.feijian.diy.AnnotationPointCut"/>
<!--开启注解支持!-->
<aop:aspectj-autoproxy/>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端