栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

代理模式 spring简单应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

代理模式 spring简单应用

需求

当前有M个场景S1,S2…Sm,他们有相同的行为和方法,假设有N种方法,所以总共有如下

  1. M 个类
  2. 每个类有N个方法

在外部每次调用接口时候,都会根据入参来决定场景是哪个?所以说决定场景的是方法级别。

现在我们需要一个类提供外部使用,他需要满足下面的要求

  1. 上述的N中方法
  2. 根据不同的不同的场景来决定是哪个服务?

相同的行为,不同的场景实现不同,对外一个共同的接口,一看就知道使用设计模式中的Proxy模式,那就我们先看看代理模式的简单UML图吧(图侵删:参考 https://www.runoob.com/design-pattern/proxy-pattern.html)

假设我们有个共同的类,定义了行为

public interface IActionService {

    List selectByPage(QueryBean queryBean);
}

他有两个实现类:DBActionServiceImpl

public class DBActionServiceImpl implements IActionService {

    @Override
    public List selectByPage(QueryBean queryBean) {
        return Collections.singletonList("db");
    }
}

和 OdpsActionServiceImpl

public class OdpsActionServiceImpl implements IActionService {

    @Override
    public List selectByPage(QueryBean queryBean) {
        return Collections.singletonList("odps");
    }
}

在代理类 NormalActionServiceProxy

    private final DBActionServiceImpl dbActionService;
    private final OdpsActionServiceImpl odpsActionService;

    public NormalActionServiceProxy(DBActionServiceImpl dbActionService,
                                    OdpsActionServiceImpl odpsActionService) {
        this.dbActionService = dbActionService;
        this.odpsActionService = odpsActionService;
    }

    @Override
    public List selectByPage(QueryBean queryBean) {
    	// 根据入参决定使用那个实现
        boolean enableMc = queryBean.enableMc();
        return enableMc ? odpsActionService.selectByPage(queryBean)
                : dbActionService.selectByPage(queryBean);
    }

外部是需要使用 NormalActionServiceProxy就好了,完全不用关心代理内部的信息
这是最简单的代理,但是通常我们是不可能这么写的。因为有N中方法,每个方法中根据参数判断使用哪个实现类过于繁琐了。
此时的结构为:

众所周时,关键字为: 接口代理,方法级别,代理的时候,那么自然而然的使用cglib代理了。

cglib代理
  1. 定义一个方法拦截器,interceptor,代码如下:ActionMethodInterceptor
public class ActionMethodInterceptor implements MethodInterceptor {
  	private final DBActionServiceImpl dbActionService;
    private final OdpsActionServiceImpl odpsActionService;

    public ActionMethodInterceptor(DBActionServiceImpl dbActionService,
                                   OdpsActionServiceImpl odpsActionService) {
        this.dbActionService = dbActionService;
        this.odpsActionService = odpsActionService;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] params, MethodProxy methodProxy) throws Throwable {
        boolean isMc = false;
        Optional any = Stream.of(params)
                .filter(it -> it instanceof IEnableMC)
                .findAny();
        if (any.isPresent()) {
            isMc = ((IEnableMC) any.get()).enableMc();
        }
        return isMc ? methodProxy.invoke(odpsActionService, params)
                : method.invoke(dbActionService, params);
    }
}
 

测试代码:

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(IActionService.class);
         DBActionServiceImpl dbActionService = new DBActionServiceImpl();
        OdpsActionServiceImpl odpsActionService = new OdpsActionServiceImpl();
        enhancer.setCallback(new ActionMethodInterceptor(dbActionService, odpsActionService));
        IActionService iActionService = (IActionService) enhancer.create();
        QueryBean queryBean = new QueryBean();
        queryBean.setNo("001");
        System.out.println(String.join(",", iActionService.selectByPage(queryBean)));
        queryBean.setNo("mc-001");
        System.out.println(String.join(",", iActionService.selectByPage(queryBean)));
    }
基于spring

基于spring 托管的话,我们需要将代理的生成托管给spring容器,方便我们注册,我们需要增加一个configuration注入bean,代码如下:

@Configuration
public class ProxyConfiguration {

    @Bean("ActionServiceProxy")
    IActionService actionServiceProxy() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(IActionService.class);
        enhancer.setCallback(actionMethodInterceptor());
        return (IActionService) enhancer.create();
    }
    @Bean
    DBActionServiceImpl dbActionService() {
        return new DBActionServiceImpl();
    }
    @Bean
    OdpsActionServiceImpl odpsActionService() {
        return new OdpsActionServiceImpl();
    }
    @Bean
    ActionMethodInterceptor actionMethodInterceptor() {
        return new ActionMethodInterceptor(dbActionService(), odpsActionService());
    }
}

这样就可以在对应的组件中使用了

@Service
public class AcService {
    @Autowired
    @Qualifier("ActionServiceProxy")
    private IActionService iActionService;
    
}

这就完成了使用cglib对接口基于method颗粒度的代理了。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号