使用xml的方式配置aop

使用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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用XML配置进行AOP编程时,需要使用以下步骤: 1. 定义切面类:创建一个Java类,用于实现切面的功能,例如日志记录、性能监控等。 2. 定义切点:创建一个切点,用于指定需要被切入的目标方法,可以使用正则表达式、通配符等方式进行匹配。 3. 定义通知:创建一个通知,用于实现切面类中定义的具体功能,通知可以分为前置通知、后置通知、异常通知、最终通知等。 4. 创建切面:将切点和通知组合在一起,形成一个切面。 5. 配置AOP:在XML配置文件中,使用<aop:config>标签指定切面类所在的包路径,并使用<aop:aspect>标签定义切面,其中包含切点和通知的定义。 6. 配置目标对象:在XML配置文件中,使用<aop:advisor>标签将切面应用到目标对象上。 示例代码: ```xml <aop:config> <aop:aspect id="logAspect" ref="logAspectBean"> <aop:pointcut id="serviceMethod" expression="execution(* com.example.service.*.*(..))"/> <aop:before pointcut-ref="serviceMethod" method="beforeMethod"/> </aop:aspect> </aop:config> <bean id="logAspectBean" class="com.example.aspect.LogAspect"/> <bean id="serviceBean" class="com.example.service.ServiceImpl"> <aop:advisor advice-ref="logAspect" pointcut-ref="serviceMethod"/> </bean> ``` 在上面的示例中,定义了一个切面类LogAspect,其中包含了一个切点serviceMethod和一个前置通知beforeMethod。然后,在配置文件中使用<aop:aspect>标签定义了切面,并将其应用到目标对象ServiceImpl上。最后,创建了一个ServiceImpl的bean,并将切面添加到该bean上。 这是一种基本的XML配置方式进行AOP编程的方法。当然,还有其他更灵活的方式,例如使用注解方式或者使用Java配置方式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值