Java——SpringAOP中XML配置

AOP与Spring配置详解
本文深入探讨了AOP的概念及其在Spring框架中的XML配置方式,包括连接点、切入点、通知、目标对象、织入、代理和切面等关键概念,并提供了具体的配置示例。

一.概念

  1. 连接点:
    如service中的方法就是连接点
  2. 切入点
    a) Service方法被增强的就是切入点
  3. 通知
    在这里插入图片描述
    a)
    b) 引介
  4. 目标对象——代理目标对象
  5. 织入——指的是代理新对象的过程
  6. 代理——一个类被AOP织入增强后,产生的代理类
  7. 切面——切入点和通知的结合

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

    <!-- an HTTP Session-scoped bean exposed as a proxy -->
    <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
        <!-- instructs the container to proxy the surrounding bean -->
        <aop:scoped-proxy/>
    </bean>

    <!-- a singleton-scoped bean injected with a proxy to the above bean -->
    <bean id="userService" class="com.foo.SimpleUserService">
        <!-- a reference to the proxied userPreferences bean -->
        <property name="userPreferences" ref="userPreferences"/>
    </bean>
</beans>

pom.xml配置:spring和aspectj

在这里插入图片描述

1. <aop:config >标签表示配置AOP

<aop:aspect > 配置切面

i. Id属性:给切面提供一个唯一标识
ii. ref属性:指定通知类bean的id

a. <aop:before >配置前置通知
  1. method属性:通知的方法

  2. Pointcut属性 :execution(切入点)

  3. Pointcut-ref——应用Pointcut表达式
    1.在这里插入图片描述
    2. 在这里插入图片描述

  4. 通配写法 *表示一个参数 ;… 表示可有可无
    1. 在这里插入图片描述
    2. 在这里插入图片描述
    3. iii. 数据类型java.lang.String 或int

  5. <aop: point> ——切入点表达式,改用全局的放到aspect
    1. 在这里插入图片描述
    2. 切面示例
    在这里插入图片描述

通知使用示例

在这里插入图片描述

2.环绕通知

1.xml配置,在aspect里面

在这里插入图片描述

2. 方法使用

使用了 Aspectj 包在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值