一、Aop 简介
- AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.
- AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点.
- 在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
二、Aop的好处
- 每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级
- 业务模块更简洁, 只包含核心业务代码.
三、添加依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- spring aop支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- aspectj支持 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
四、配置方式
-
基于代理的经典AOP
-
基于代理工厂
1.新建通知对象
2.把通知对象放到spring 容器
3.根据以下配置代理工厂
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="xxx" /> <!-- 代理bean工厂中完成织入 --> <bean id="名称" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="目标" /> <property name="interceptorNames"> <list> <value>通知对象</value> </list> </property> </bean> </beans>
-
自动代理方式
1.新建通知对象
2.把通知对象放到spring 容器
3.根据自动代理配置
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="xxx" /> <!-- 自动代理 --> <aop:aspectj-autoproxy /> <!-- Advisor 通知代理者 --> <bean id="名称" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="pattern"> <value>表达式</value> </property> <property name="advice" ref="通知" /> </bean> </beans>
-
-
基于Aspectj 表达式配置AOP(推荐使用)
-
基于配置文件
1.添加切面对象(aspect对象)
2.把切面对象交给spring
3.配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="com.chair" /> <aop:config> <!-- 配置切入点 --> <aop:pointcut id="切点id" expression="Aspectj 表达式" /> <!-- 配置切面 把增强用到方法上面 --> <aop:aspect ref="切面对象id"> <!-- 前置通知 --> <aop:before method="切面对象方法" pointcut-ref="切点id" /> <!-- 后置通知 --> <aop:after-returning method="切面对象方法" pointcut-ref="切点id" /> <!-- 异常通知 --> <aop:after-throwing method="切面对象方法" pointcut-ref="切点id" /> <!-- 环绕通知 --> <aop:around method="切面对象方法" pointcut-ref="切点id" /> </aop:aspect> </aop:config> </beans>
-
基于注解配置
1.添加切面对象(aspect对象)(加上注解)
2.把切面对象交给spring
3.配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="com.chair" /> <!-- 自动代理 --> <aop:aspectj-autoproxy /> </beans>
-
五、常用通知
@Before[MethodBeforeAdvice]: 前置通知, 在方法执行之前执行
@After [AfterReturningAdvice]: 返回通知, 在方法返回结果之后执行
@AfterThrowing [ThrowsAdvice]: 异常通知, 在方法抛出异常之后
@Around[MethodInterceptor]: 环绕通知, 围绕着方法执行
六、Aspectj表达式详解
https://blog.csdn.net/xubo_ob/article/details/78182014