Spring之AOP适配器模式

Spring之AOP适配器模式,第1张

  • @author 波波烤鸭

  • @email dengpbs@163.com

*/

public interface SomeService {

public void doSome();

}

/**

  • 目标对象

  • @author 波波烤鸭

  • @email dengpbs@163.com

*/

public class SomeServiceImpl implements SomeService {

@Override

public void doSome() {

System.out.println(“目标对象…方法执行了”);

}

}

[](()3.创建通知

/**

  • 前置通知

  •   需要实现MethodBeforeAdvice接口
    
  • @author 波波烤鸭

  • @email dengpbs@163.com

*/

public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

/**

  • method 目标方法

  • args 目标方法参数列表

  • target 目标对象

*/

@Override

public void before(Method method, Object[] args, Object target) throws Throwable {

System.out.println(“前置通知执行了…”);

}

}

[](()4.配置文件

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:p=“http://www.springframework.org/schema/p”

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">

myMethodBeforeAdvice

[](()5.测试

@Test

public void test1() {

ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);

// 注意通过getBean获取增强的代理类!!!

SomeService some = ac.getBean(“proxyFactoryBean”,SomeService.class);

some.doSome();

}

输出:

说明我们配置的前置通知生效了,在目标方法执行之前执行了。

[](()二、适配器应用解析


[](()1.Advice体系结构

说明:

  1. advice的类型有:BeforeAdvice,AfterReturningAdvice,ThrowsAdvice等

  2. 每个类型的通知都有对应的拦截器

| advice | 拦截器 |

| — | :-- |

| BeforeAdvice | MethodBeforeAdviceInterceptor |

| AfterAdvice | AfterReturningAdviceInterceptor |

| AfterAdvice | ThrowsAdviceInterceptor |

  1. Spring容器需要将每个具体的advice封装成对应的拦截器,返回给容器,这里对advice转换就需要用到适配器模式。
[](()2.适配器的实现

以前置通知为例

[](()2.1Adaptee

MethodBeforeAdvice

public interface 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 MethodBeforeAdvice extends BeforeAdvice {

void before(Method method, Object[] args, Object target) throws Throwable;

}

[](()2.2target

Adapter的接口 AdvisorAdapter

public interface AdvisorAdapter {

// 判断通知类型是否匹配

boolean supportsAdvice(Advice advice);

// 获取对应的拦截器

MethodInterceptor getInterceptor(Advisor advisor);

}

[](()2.3Adapter

MethodBeforeAdviceAdapter

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {

@Override

public boolean supportsAdvice(Advice advice) {

return (advice instanceof MethodBeforeAdvice);

}

@Override

public MethodInterceptor getInterceptor(Advisor advisor) {

MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();

// 通知类型匹配对应的拦截器

return new MethodBeforeAdviceInterceptor(advice);

}

}

[](()2.4Client

代理类通过DefaultAdvisorAdapterRegistry类来注册相应的适配器。

public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable {

private final List adapters = new ArrayList(3);

/**

  • Create a new DefaultAdvisorAdapterRegistry, registering well-known adapters.

*/

public DefaultAdvisorAdapterRegistry() {

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/726090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存