在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 如何模仿 WebMvcConfigurerAdapter 靈活的添加配置

如何模仿 WebMvcConfigurerAdapter 靈活的添加配置

  1. 問題描述

我在寫一個框架,但是主要功能是下位機與上位機交互的協(xié)議類似json雙方都可以靈活的通過幾個索引表中尋找字段(例如這串二進制數(shù)據(jù)描述了一個業(yè)務模型,我需要將這串二進制數(shù)據(jù)解析成一個Java對象),為了框架的使用的靈活性,我想模仿Spring中的WebMvcConfigurerAdapter但是現(xiàn)在遇到了一個問題,我寫出來的東西特別復雜,就不貼出來了,在此求助各路大神。

可以引用spring框架

類似以下這種配置方式

@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
    /**
     * 配置靜態(tài)訪問資源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");
        super.addResourceHandlers(registry);
    }
}
  1. 最終的目的
@Configuration
public class ProtocolIndexConfigurerAdapterImpl extends ProtocolIndexConfigurerAdapter {
    /**
     * 配置靜態(tài)訪問資源
     * @param registry
     */
    @Override
    public void addIndex(ProtocolIndexRegistry registry) {
       // 0001是在二進制串中描述某字段的意義,
       register.addTypeIndex("0001").register("id");
       register.addTypeIndex("0002").register("name");
    }
}
回答
編輯回答
吃藕丑

更新2:

突然想起來,你這個更類似攔截器、過濾器的配置啊,可以參考一下。


更新:

interface ProtocolIndexConfigurer { void addIndex(ProtocolIndexRegistry registry); }

abstract class ProtocolIndexConfigurerRegister {
    
    // 這里假設 registry 不是全局的,否則 configurer.addIndex(registry) 的調用形式很突兀
    // 全局的話應該是 registry.addConfigurer(configurer) 這個你可以考慮一下
    public abstract void register();
    
}

使用:

@Component("conf1")
class ProtocolIndexConfigurer1 implements ProtocolIndexConfigurer {

    @Override
    public void addIndex(ProtocolIndexRegistry registry) { 
        // 注冊行為...
    }
    
}

@Component("conf2")
class ProtocolIndexConfigurer2 implements ProtocolIndexConfigurer { ... }

class MyProtocolIndexConfigurerRegister extends ProtocolIndexConfigurerRegister {

    @Resource("conf1")
    private ProtocolIndexConfigurer conf1; // 注入,或者用 @bean 導入都行
    
    @Resource("conf2")
    private ProtocolIndexConfigurer conf2;
    
    @Override
    public void register() {
        conf1.addIndex(new ProtocolIndexRegistry());
        conf2.addIndex(new ProtocolIndexRegistry());
        // 如果 ProtocolIndexRegistry 是全局的,那么就可以是
        // registry.addConfigurer(conf1)
                    .addConfigurer(conf2)
                    ...
        // 但是這樣改動可能比較大
    }
}

原答案:

interface ProtocolIndexConfigurer { void addIndex(ProtocolIndexRegistry registry); }

@ConditionOnMissingBean(name = "example")
@Component
class ProtocolIndexConfigurerAdapter implements ProtocolIndexConfigurer {
    @Override
    public void addIndex(ProtocolIndexRegistry registry) { /* 空實現(xiàn) */ }
}

class SomewhereInjectProtocolIndexRegistry {
    
    @Autowired
    private final ProtocolIndexConfigurerAdapter adapter;
    
    protected void func() {
        ProtocolIndexRegistry registry = // get registry
        adapter.addIndex(registry);
        // save index mapping
    }
}

使用:

@Bean(name = "example")
public SomeAdapter extends ProtocolIndexConfigurerAdapter {
    @Override
    public void addIndex(ProtocolIndexRegistry registry) {
        // do something here.
    }
}

印象里是這樣,你可以試一下,主要是 @ConditionOnxxx 注解的使用,給個空的默認實現(xiàn)避免找不到實現(xiàn)類拋異常。

2017年12月30日 02:48