Weld CDI自定义范围中的种子值

Jan*_*ing 7 cdi weld weld2

来自Guice背景,我知道可以使用范围从范围中播种对象值.

  scope.seed(Key.get(SomeObject.class), someObject);
Run Code Online (Sandbox Code Playgroud)

我想通过注册一个从a获取值的Bean可以做到这一点AbstractBoundContext,但是从自定义范围中播种一个值的示例似乎很难找到.如何创建一个自定义范围,该范围可以为可以注入其他位置的值提供种子?

编辑: 我目前正在使用以下解决方法,可以在拦截器中注入以设置Configuration何时进入范围,然后可以通过其线程本地提供程序注入.我仍然在寻找那些感觉不那么hacky /与Weld中的范围/范围上下文系统更加集成的选项.

@Singleton
public class ConfigurationProducer {

    private final InheritableThreadLocal<Configuration>  threadLocalConfiguration =
    new InheritableThreadLocal<>();

    @Produces
    @ActiveDataSet
    public ConfigurationConfiguration() {
       return threadLocalConfiguration.get()
    }

    public void setConfiguration(Configuration configuration) {
         threadLocalConfiguration.set(configuration);
    }    

}
Run Code Online (Sandbox Code Playgroud)

Jan*_*ing 1

答案是使用 AfterBeanDiscovery 事件注册一个自定义 bean,如下所示:

    event.addBean()
        .createWith(ctx -> commandContext.getCurrentCommandExecution())
        .addType(CommandExecution.class)
        .addQualifier(Default.Literal.INSTANCE)
        .scope(CommandScoped.class)
        .beanClass(CommandExtension.class);
Run Code Online (Sandbox Code Playgroud)

https://github.com/weld/command-context-example有一个相当复杂的示例