Spring AOP声明的三种方式(JDK1.4及以下)

本文介绍了Spring框架中不同版本的AOP配置方法,包括Spring1.X的传统方式及Spring2.X基于Schema的支持,并展示了如何使用AspectJ进行切面定义。

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

1、Spring 1.X中使用的传统方式
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    
<!--CONFIG-->
    
<bean id="aopTest1"
        class
="org.springframework.aop.framework.ProxyFactoryBean">
        
<property name="proxyInterfaces">
            
<value>org.readyesb.web.services.dataprovider.interfaces.DataProvider</value>
        
</property>
        
<property name="target">
            
<ref local="beanTarget" />
        
</property>
        
<property name="interceptorNames">
            
<list>
                
<value>theAdvisor</value>
            
</list>
        
</property>
    
</bean>

    
<!--CLASS-->
    
<bean id="beanTarget" class="org.readyesb.web.services.dataprovider.impl.DataProviderImpl"/>

    
<!--ADVISOR-->
    
<!--Note: An advisor assembles pointcut and advice-->
    
<bean id="theAdvisor"
        class
="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        
<property name="advice">
            
<ref local="theThrowAdvice" />
        
</property>
        
<property name="patterns">
            
<value>.*org.readyesb.web.services.dataprovider.interfaces.DataProvider.*</value>
        
</property>
    
</bean>

    
<!--ADVICE-->
    
<bean id="theThrowAdvice"
        class
="org.readyesb.exception.FaultException" />
</beans>

2、Spring 2.X Schema-based AOP support
<?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-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>

    
<aop:config>
        
<aop:aspect id="aopTestId" ref="aopTestException">
            
<aop:pointcut id="theExceptionAopTestMethod" expression="execution(* org.readyesb.web.services.dataprovider.impl.DataProviderImpl.*(..))"/>
            
<aop:after-throwing pointcut-ref="theExceptionAopTestMethod" method="afterThrowing"/>
        
</aop:aspect>
    
</aop:config>
    
<bean id="aopTestException" class="org.readyesb.exception.FaultException"/>
</beans>

3、Spring 2.X Schema-based AOP support - Advisors
<?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-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>

    
<aop:config>
        
<aop:advisor id="aopTest" advice-ref="aopTestException" pointcut="execution(* org.readyesb.web.services.dataprovider.impl.DataProviderImpl.*(..))"/>
    
</aop:config>
    
<bean id="aopTestException" class="org.readyesb.exception.FaultException"/>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值