代理模式
SpringAOP的底层是代理模式。
代理模式可以分为:
- 静态代理
- 动态代理
静态代理
静态代理的角色一般分为以下几个:
- 抽象角色:一般使用接口或者抽象类来解决
- 真实角色:被代理的角色
- 代理角色:代理真实角色,代理真实角色后,一般还会做一些附属的操作
- 客户:访问代理对象的人
代理模式的好处:
- 可以使真实角色的操作更加纯粹(不用去关注一些公共的业务)
- 公共业务交给代理角色(实现了业务的分工)
- 公共业务发生扩展的时候,方便集中管理
代理模式的缺点:
- 一个真实角色就会产生一个代理角色;代码量会翻倍,降低开发效率
动态代理
动态代理的角色和静态代理的角色是一样的,只是动态代理的代理类是动态生成的,并不是直接写好的。
动态代理可分为两大类:基于接口的动态代理和基于类的动态代理。
-
基于接口的动态代理可使用 JDK动态代理
-
基于类的动态代理可以使用 cglib
动态代理的好处:
- 可以使真实角色的操作更加纯粹(不用去关注一些公共的业务)
- 公共业务交给代理角色(实现了业务的分工)
- 公共业务发生扩展的时候,方便集中管理
- 一个动态代理类代理的是一个接口,一般就是对应的一类业务
- 一个动态代理类可以代理多个类,只要实现同一个接口即可
AOP
AOP:Aspect Oriented Programming,意为 面向切面编程,它是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,它是软件开发中的一份热点,也是Spring框架中的一个重要的内容,是函数式编程的一种衍生范式。利用AOP可以对业务逻辑的各个部分进行隔离,从而使业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。
AOP在Spring中的作用
AOP提供声明式事务,允许用户自定义切面。
- 横切关注点:跨越应用程序多个模块的方法或者功能,即 与我们的业务逻辑无关的,但是我们需要关注的部分,就是横切关注点,比如 日志、事务、缓存、安全等等。
- 切面(aspect):横切关注点,被模块化的对象,它是一个类。
- 通知(advice):切面必须要完成的工作,它是类中的一个方法。
- 目标(target):被通知对象。
- 代理(proxy):向目标对象应用通知之后创建的对象。
- 切入点(pointcut):切面通知执行的地点的定义
- 连接点(jointpoint):与切入点匹配的执行点。
SpringAOP中,通过advice定义横切逻辑,Spring中支持5中类型的advice。
通知类型 | 连接点 | 实现接口 |
---|---|---|
前置通知 | 方法前 | org.springframework.aop.MethodBeforeAdvice |
后置通知 | 方法后 | org.springframework.aop.AfterReturningAdvice |
环绕通知 | 方法前后 | org.aopalliance.intercept.MethodInterceptor |
异常抛出通知 | 方法抛出异常 | org.springframework.aop.ThrowsAdvice |
引介通知 | 类中增加新的方法属性 | org.springframework.aop.IntroductionInterceptor |
AOP 在不改变原有代码的情况下,增加新的功能。
使用Spring实现AOP
使用Spring实现AOP由三种方式:
- 使用Spring的API接口实现
- 使用自定义来实现AOP,主要是切面定义
- 使用注解实现
搭建环境
1、新建一个项目
2、在pom文件中导入以下依赖(使用AOP织入,需要导入一个依赖包)
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
3、在resources下新建一个applicationContext.xml文件
4、编写一个UserService接口
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
5、编写UserService接口的实现类UserServiceImpl
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("更新用户");
}
public void search() {
System.out.println("查询用户");
}
}
方式1
使用Spring的API接口实现:
编写日志类Log(用于方法前)
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"方法执行了 ");
}
}
编写日志类LogAfter(用于方法后)
public class LogAfter implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法,返回了"+returnValue);
}
}
在applicationContext.xml中配置如下:
<bean id="userService" class="com.llx.service.impl.UserServiceImpl"/>
<bean id="log" class="com.llx.config.Log"/>
<bean id="afterlog" class="com.llx.config.LogAfter"/>
<aop:config>
<aop:pointcut id="pc-userservice" expression="execution(* com.llx.service.impl.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pc-userservice"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pc-userservice"/>
</aop:config>
测试
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
方式2
使用自定义来实现AOP:
编写一个DiyPointCut类:
public class DiyPointCut {
public void before(){
System.out.println("=========方法执行前==========");
}
public void after(){
System.out.println("=========方法执行后==========");
}
}
在applicationContext.xml中配置如下:
<bean id="userService" class="com.llx.service.impl.UserServiceImpl"/>
<bean id="diy" class="com.llx.config.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="pc-userservice" expression="execution(* com.llx.service.impl.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pc-userservice"/>
<aop:after method="after" pointcut-ref="pc-userservice"/>
</aop:aspect>
</aop:config>
测试:
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
方式3
使用注解实现:
编写一个AnnotationPointCut类:
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.llx.service.UserService.*(..))")
public void before(){
System.out.println("=========方法执行前==========");
}
@After("execution(* com.llx.service.UserService.*(..))")
public void after(){
System.out.println("=========方法执行后==========");
}
@Around("execution(* com.llx.service.UserService.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
System.out.println(joinPoint.getSignature());
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
在applicationContext.xml中配置如下:
<bean id="userService" class="com.llx.service.impl.UserServiceImpl"/>
<bean id="annotationPointCut" class="com.llx.config.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
测试:
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}