使用xml的方式配置aop

本文详细介绍了如何使用XML配置方式实现面向切面编程(AOP)。通过具体示例,展示了如何定义切点、切面及各种通知类型,包括前置、后置、返回、异常和环绕通知,帮助读者深入理解AOP的配置细节。

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

使用xml的方式配置aop

需要在xml文件头添加命名空间

xmlns:aop="http://www.springframework.org/schema/aop"

   http://www.springframework.org/schema/aop 
   https://www.springframework.org/schema/aop/spring-aop.xsd"

有些麻烦


    <bean id="logUtilg" class="com.mashibing.util.LogUtil"></bean>
    <bean id="securityUtil" class="com.mashibing.util.SecurityUtil"></bean>
    <bean id="mycalculator" class="com.mashibing.service.Mycalculator"></bean>
<!--设置aop的xml配置方法-->
    <aop:config>
        <!--抽象表达式-->
        <aop:pointcut id="mycal" expression="execution(public * com..*(..))"></aop:pointcut>
        <!--设置切面    ,关联bean -->
        <aop:aspect ref="logUtilg">
        	<!--设置通知-->
            <aop:before method="start" pointcut="execution(Integer com..*(..))"></aop:before>
            <aop:after-returning method="stop" pointcut="execution(* com..*(..))" returning="result"></aop:after-returning>
            <aop:after method="logFinally" pointcut="execution(Integer com.mashibing.service.Mycalculator.*(..))" ></aop:after>
            <aop:after-throwing method="logException" pointcut="execution(* com..*(..))" throwing="e"></aop:after-throwing>
            <aop:around method="around" pointcut="execution(* com..*(..))" ></aop:around>
        </aop:aspect>


        <aop:aspect ref="securityUtil">
            <aop:after method="logFinally" pointcut-ref="mycal"></aop:after>
            <aop:after-throwing method="logException" pointcut-ref="mycal" throwing="e"></aop:after-throwing>
            <aop:before method="start" pointcut-ref="mycal"></aop:before>
            <aop:after-returning method="stop" pointcut-ref="mycal" returning="result"></aop:after-returning>
        </aop:aspect>

    </aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值