Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect

本文详细介绍了Spring中基于XML配置的AOP实现方式,包括如何使用<aop:config>来组织切面、切入点及通知等元素。通过具体示例展示了如何定义切面类、业务类,并进行AOP配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect

 

    Spring所有的切面和通知器都必须放在一个<aop:config>内(可以配置包含多个<aop:config>元素),每个<aop:config>包含pointcut,advisor和apsect元素。ps:他们必须按照这个顺序进行声明

  • <aop:pointcut>:用来定义切入点,该切入点可以重用;
  • <aop:advisor>:用来定义只有一个通知和一个切入点的切面;
  • <aop:aspect>:用来定义切面,该切面可以包含多个切入点和通知,而且标签内部的通知和切入点定义是无序的;和advisor的区别就在此,advisor只包含一个通知和一个切入点。
<aop:config>AOP定义开始
       <aop:pointcut/>     切入点定义(0或多个)
       <aop:advisor/>     advisor定义(0或多个)
       <aop:aspect>     切面定义开始(0或多个)
                <aop:pointcut/>           切入点定义(0或多个)
                 <aop:before/>           前置通知(0或多个)
                 <aop:after-returning/>           返回后通知(0或多个)
                 <aop:after-throwing/>           抛出异常后通知(0或多个)
                 <aop:after/>           后通知(0或多个)
                 <aop:around/>           环绕通知(0或多个)
                    <aop:declare-parents/>           引入定义(0或多个)
         <aop:aspect>     切面定义结束
</aop:config>AOP定义结束

 

    <aop:config>风格的配置大量使用了Spring的自动代理机制.

实现:

     <aop:config>
          <aop:aspect id="myAspect" ref="aBean">
             ...
          </aop:aspect>
     </aop:config>
     
     <bean id="aBean" class="...">
           ...
     </bean>

 

例子:

新建2个类

package com.aop.schema;
/**
 * 
 * 切面类
 *
 */
public class MyAspect {

}

 

package com.aop.schema;

/**
 * 
 * 业务类
 *
 */
public class ApsectBiz {

}

 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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
         
     <bean id="myAspect" class="com.aop.schema.MyAspect"></bean>
      
     <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean>
      
     <aop:config>
          <aop:aspect id="myAspectAOP" ref="myAspect">
            
          </aop:aspect>
     </aop:config>
 
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值