Java——SpringAOP中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 包在这里插入图片描述

发布了25 篇原创文章 · 获赞 25 · 访问量 1053

猜你喜欢

转载自blog.csdn.net/Android_Cob/article/details/105142396