Spring AOP
Aop 简介
- AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.
- AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点.
- 在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
Aop的有点
- 每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级
- 业务模块更简洁, 只包含核心业务代码.
Maven项目依赖添加
<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>
配置方式
1.基于代理的经典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: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>
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"
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-4.3.xsd">
<!-- spring扫描包 -->
<context:component-scan base-package="com.J200106.test4" />
<!-- 代理bean工厂中完成织入 -->
<bean id="proxy2" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="client" />
<property name="interceptorNames">
<list>
<value>通知代理者bean名称</value>
</list>
</property>
</bean>
<!-- Advisor 通知代理者 -->
<bean id="zddl1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern">
<value>表达式(指定包名.类名.方法名)</value>
</property>
<property name="advice" ref="通知名" />
</bean>
</beans>
3.基于Aspectj 表达式配置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: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>
4.基于注解配置
<?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]: 环绕通知, 围绕着方法执行
@AfterReturning在所拦截方法return后执行该注解的函数