夜光带你走进Spring(五)

版权声明:Genius https://blog.csdn.net/weixin_41987706/article/details/89311086

夜光序言:

 

人总是妄想在不能拥有的年纪,去拥有一些不能拥有的东西。

 

正文:化繁为简,力求精进

aop编程, XML配置方式

步骤:

1. 引入aop 相关jar文件

2. bean.xml  引入aop名称空间

3. Aop配置

 

<?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.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/aop

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

 

<!-- dao实例加入容器 -->

<bean id="userDao" class="cn.Genius.f_aop_xml.UserDao"></bean>

 

<!-- 实例化切面类 -->

<bean id="aop" class="cn.Genius.f_aop_xml.TransactionAop"></bean>

 

<!-- Aop相关配置 -->

<aop:config>

<!-- 切入点表达式定义 -->

<aop:pointcut expression="execution(* cn.Genius.f_aop_xml.UserDao.*(..))" id="pt"/>

 

<!-- 切面配置 -->

<aop:aspect ref="aop">

 

<!-- 【环绕通知】 -->

<aop:around method="arroud" pointcut-ref="pt"/>

 

<!-- 【前置通知】 在目标方法之前执行 -->

<aop:before method="beginTransaction" pointcut-ref="pt" />

 

<!-- 【后置通知】 -->

<aop:after method="commit" pointcut-ref="pt"/>

 

<!-- 【返回后通知】 -->

<aop:after-returning method="afterReturing" pointcut-ref="pt"/>

 

<!-- 异常通知 -->

<aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>

 

</aop:aspect>

</aop:config>

 

</beans>      

 

 

 

 

 

 

  

 

 

切入点表达式语法详解

切入点表达式:

拦截指定的类,生成代理对象~

 

execution(

modifiers-pattern?    拦截的方法的访问修饰符

ret-type-pattern                   方法返回类型,必须指定

declaring-type-pattern?             拦截的方法所在的类

 name-pattern(param-pattern)       拦截的方法(以及方法的参数列表)

throws-pattern?)                  方法声明的异常

 

总结:

拦截,一定要指定到方法~

 

 

共性问题:

1. @Override报错

Jdk1.5 只支持父类方法的重写,不支持接口

Jdk1.6 修复这个问题

修改编译编译,改为1.6

2. xml配置没有提示

猜你喜欢

转载自blog.csdn.net/weixin_41987706/article/details/89311086