Spring AOP(一)

时间:2020-10-13 16:54:39   收藏:0   阅读:25

介绍

AOP,aspect oriented programing,面向切面编程。
动态代理:基于JDK和基于第三方cglib

使用

1、配置pom.xml

<!--aop依赖jar-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

2、配置文件的约束

<?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">
</beans>

3、配置通知

<bean id="account" class="com.my.entity.Account>
<bean id="accountAspect" class="com.myentity.AccountAspect">
<!--AOP配置-->
<aop:config>
    <!--配置全局切入点-->
    <!--<aop:pointcut id="" expression=""/>-->
    <!--配置切面-->
    <aop:aspect id="myAspect" ref="accountAspect">
        <!--配置切面属性:通过通知标签描述切入点-->
        <aop:before method="myBefore" pointcut-ref="pt"/>
        <aop:after-returning method="myAfterReturning" pointcut-ref="pt"/>
        <aop:after-throwing method="myThrowing" pointcut-ref="pt"/>
        <aop:after method="myAfter" pointcut-ref="pt"/>
        <!--配置局部切入点:只能当前切面使用-->
        <aop:pointcut id="pt" expression="execution(public void com.yaroange.service.impl.UserServiceImpl.addUser(java.lang.Integer))"/>
    </aop:aspect>
</aop:config> 

切入点表达式

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!