spring 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:context="http://www.springframework.org/schema/context"
       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.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>


<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>

<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
</aop:aspect>



</aop:config>








</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOPSpring框架中的一个重要模块,它通过动态代理实现了面向切面编程的思想。下面是Spring AOPXML配置详解: 1. 配置命名空间和约束 在使用Spring AOP之前,需要在XML配置文件中声明Spring AOP的命名空间和约束,如下所示: ```xml <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"> ``` 2. 配置切面 在Spring AOP中,切面由切点和通知组成。切点定义了在哪些方法上进行拦截,通知定义了在拦截时要执行的逻辑。下面是一个切面的配置示例: ```xml <bean id="myAspect" class="com.example.MyAspect"> <property name="transactionManager" ref="txManager"/> </bean> <aop:config> <aop:aspect ref="myAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.Service.*(..))"/> <aop:before pointcut-ref="serviceMethods" method="beginTransaction"/> <aop:after-returning pointcut-ref="serviceMethods" method="commitTransaction"/> <aop:after-throwing pointcut-ref="serviceMethods" method="rollbackTransaction"/> </aop:aspect> </aop:config> ``` 上面的例子中,声明了一个名为“myAspect”的切面,并定义了一个名为“serviceMethods”的切点,拦截com.example.Service包中所有方法的执行。在拦截时,分别执行了beginTransaction()、commitTransaction()和rollbackTransaction()方法。 3. 配置通知 通知是切面中的一个组成部分,它定义了在什么时候执行切面的逻辑。Spring AOP支持五种类型的通知:前置通知、后置通知、返回通知、异常通知和环绕通知。下面是通知的配置示例: ```xml <aop:before pointcut="execution(* com.example.Service.*(..))" method="beforeAdvice"/> <aop:after-returning pointcut="execution(* com.example.Service.*(..))" method="afterReturningAdvice"/> <aop:after-throwing pointcut="execution(* com.example.Service.*(..))" method="afterThrowingAdvice"/> <aop:around pointcut="execution(* com.example.Service.*(..))" method="aroundAdvice"/> ``` 上面的例子中,分别配置了前置通知、返回通知、异常通知和环绕通知,它们都拦截com.example.Service包中的所有方法。在拦截时,分别执行了beforeAdvice()、afterReturningAdvice()、afterThrowingAdvice()和aroundAdvice()方法。 4. 配置引入 引入是Spring AOP中的一个特殊功能,它允许将额外的方法和属性添加到现有的类中,而不需要修改原始类的代码。下面是引入的配置示例: ```xml <aop:config> <aop:aspect ref="myAspect"> <aop:declare-parents types-matching="com.example.Service+" implement-interface="com.example.Transactional" default-impl="com.example.TransactionalImpl"/> </aop:aspect> </aop:config> ``` 上面的例子中,声明了一个名为“myAspect”的切面,并引入了接口com.example.Transactional,将其实现类指定为com.example.TransactionalImpl。这样,在运行时,com.example.Service类就自动实现了com.example.Transactional接口。 以上就是Spring AOPXML配置详解。在实际开发中,可以根据业务需求和实际情况选择合适的配置方式来实现切面编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值