概述
面向切面编程AOP(Aspect Oriented Programming)
- 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
- AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
- 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
Aop在Spring中的作用
提供声明式事务;允许用户自定义切面
名词解释:
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(Aspect):横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
Spring AOP 包括以下类型的通知:
类型 | 描述 |
---|
Before advice | 在连接点之前运行的通知,但没有能力阻止执行流继续到连接点(除非它抛出异常)。 | After returning advice | 在连接点正常完成后运行的通知(例如,如果方法返回而没有抛出异常)。 | After throwing advice | 如果方法通过抛出异常退出,则运行通知。 | After (finally) advice | 不管连接点退出的方式(正常或异常返回)都将运行的通知 | Around advice | 环绕连接点的通知,例如方法调用。这是最有力的建议。环绕通知可以在方法调用之前和之后执行自定义行为。它还负责选择是继续连接点还是通过返回自己的返回值或抛出异常来缩短建议的方法执行。 |
Aop 在 不改变原有代码的情况下 , 去增加新的功能
Spring实现AOP
使用AOP织入,需要导入一个依赖包aspectjweaver
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
方式一 通过SpringAPI实现
- 首先编写业务接口和实现类
UserService .java
package com.wcy.service;
public interface UserService {
void add();
void delete();
void update();
void query();
}
UserServiceImpl .java
package com.wcy.service;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("添加了一个用户!");
}
@Override
public void delete() {
System.out.println("删除了一个用户!");
}
@Override
public void update() {
System.out.println("修改了一个用户!");
}
@Override
public void query() {
System.out.println("查询了一个用户!");
}
}
然后我们需要增加一个前置功能,和一个后置功能,通过实现Spring中的接口来实现
前置功能 BeforeLog.java
- 实现接口MethodBeforeAdvice
- 即为Before advice类型
package com.wcy.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("开始执行" + method.getName() + "方法!");
}
}
后置功能 AfterLog.java
- 实现AfterReturningAdvice接口
- 即为After returning advice类型
package com.wcy.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName()+"执行完毕!返回值为:"+returnValue);
}
}
接下来只要在配置文件中配置即可 首先要导入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"
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">
</beans>
配置
<bean id="userService" class="com.wcy.service.UserServiceImpl"/>
<bean id="before" class="com.wcy.log.BeforeLog"/>
<bean id="after" class="com.wcy.log.AfterLog"/>
<aop:config>
<aop:pointcut id="userServiceO1" expression="execution(* com.wcy.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="before" pointcut-ref="userServiceO1"/>
<aop:advisor advice-ref="after" pointcut-ref="userServiceO1"/>
</aop:config>-->
测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
方式二 自定义实现AOP
业务代码和实现类不变,我们想自己来实现同样的功能
自定义的前置通知和后置通知
package com.wcy.log;
public class DiyLog {
public void before(){
System.out.println("方法开始执行!");
}
public void after(){
System.out.println("方法执行之后!");
}
}
在配置文件中注册
<bean id="userService" class="com.wcy.service.UserServiceImpl"/>
<bean id="diyLog" class="com.wcy.log.DiyLog"/>
<aop:config>
<aop:aspect id="diy" ref="diyLog">
<aop:pointcut id="userServiceO1" expression="execution(* com.wcy.service.UserService.*(..))"/>
<aop:before method="before" pointcut-ref="userServiceO1"/>
<aop:after method="after" pointcut-ref="userServiceO1"/>
</aop:aspect>
</aop:config>
测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
方式三 使用注解实现AOP
这里的额外增强类,还是自己写的 注解中的表达式和配置文件一样
package com.wcy.log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationDiyLog {
@Before("execution(* com.wcy.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("执行前!");
}
@After("execution(* com.wcy.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("执行后!");
}
@Around("execution(* com.wcy.service.UserServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前!");
System.out.println(pjp.getSignature());
Object proceed = pjp.proceed();
System.out.println("环绕后!");
return proceed;
}
}
最后再配置文件中注册,增加支持注解的配置即可
<bean id="annotationDiyLog" class="com.wcy.log.AnnotationDiyLog"/>
<aop:aspectj-autoproxy/>
aop:aspectj-autoproxy:说明
通过aop命名空间的<aop:aspectj-autoproxy /> 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy /> 隐藏起来了
<aop:aspectj-autoproxy /> 有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/> 时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
|