当前位置: 首页 > article >正文

(注解配置AOP)学习Spring的第十七天

 基于注解配置的AOP

来看注解式开发 : 

先把目标与通知放到Spring里管理  :


@Service("userService")
public class UserServiceImpl implements UserService {
    @Override
    public void show1() {
        System.out.println("show1......");
    }

    @Override
    public void show2() {
        System.out.println("show2......");
    }
}

 看这个通知 ,加@Aspect开始编辑织入 , @Before()里放的是切入点配置:

@Component
@Aspect
public class MyAdvice {
    //  <aop:before method="beforeAdvice" pointcut-ref="execution(* com.itheima.service.impl.*.*(..))"/>
    @Before("execution(* com.itheima.service.impl.*.*(..))")
    public void beforeAdvice() {

        System.out.println("前置的增强....");
    }

}

还需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
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       ">

    <!--组件扫描-->
    <context:component-scan base-package="com.itheima"/>

    <!--使用注解配置AOP,需要开启AOP自动代理-->
    <aop:aspectj-autoproxy/>




    </beans>

 如此 , 注解配置就完成了

下图是整体配置信息对比

二 . 切点表达式的抽取

用@Poincut代替了

execution(* com.itheima.service.impl.*.*(..)) , 是操作更加方便

代码如下 :

 @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    public void myPoincut(){}




    //  <aop:before method="beforeAdvice" pointcut-ref="execution(* com.itheima.service.impl.*.*(..))"/>
    @Before("MyAdvice.myPoincut()")
    public void beforeAdvice() {

        System.out.println("前置的增强....");
    }

三 .代替xml配置方式

新建一个SpringConfig配置类来管理扫描注解

代码如下

@Configuration
@ComponentScan("com.itheima")    // <context:component-scan base-package="com.itheima"/>
@EnableAspectJAutoProxy   //<aop:aspectj-autoproxy/>
public class SpringConfig {


}


http://www.kler.cn/news/234141.html

相关文章:

  • 每日五道java面试题之java基础篇(五)
  • 43.1k star, 免费开源的 markdown 编辑器
  • grafana+prometheus+hiveserver2(jmx_exporter+metrics)
  • [AIGC] Spring Gateway:一个简单 yet powerful API 网关
  • 每日五道java面试题之java基础篇(二)
  • Go内存优化与垃圾收集
  • 安全之护网(HVV)、红蓝对抗
  • 蓝桥杯官网练习题(Excel地址)
  • 【Chrono Engine学习总结】3-地型terrain
  • Linux基础-配置网络
  • 数据分析基础之《pandas(7)—高级处理2》
  • hook函数——useRef
  • 无人机应用场景和发展趋势,无人机技术的未来发展趋势分析
  • ubuntu22.04安装部署03: 设置root密码
  • 机器学习简介
  • 2.9 Binance_interface APP 现货交易-限单价平仓
  • MySQL数据库-索引概念及其数据结构、覆盖索引与回表查询关联、超大分页解决思路
  • 人工智能之参数估计
  • 算法学习——LeetCode力扣栈与队列篇1
  • 计算机视觉主要知识点
  • CleanMyMac X 4.14.7帮您安全清理Mac系统垃圾
  • [UI5 常用控件] 08.Wizard,NavContainer
  • 论文笔记:相似感知的多模态假新闻检测
  • Nginx实战:3-日志按天分割
  • python coding with ChatGPT 打卡第19天| 二叉树:合并二叉树
  • 显示器校准软件:BetterDisplay Pro for Mac v2.0.11激活版下载
  • 读书笔记之《运动改造大脑》:运动是最佳的健脑丸
  • 【大厂AI课学习笔记】【1.6 人工智能基础知识】(1)人工智能、机器学习、深度学习之间的关系
  • 二、数据结构
  • golang 集成sentry:http.Client