CDI:从外部库向bean注入资源 [英] CDI: Injecting resources to beans from external libraries

查看:94
本文介绍了CDI:从外部库向bean注入资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring中,我们有基于注释和基于XML的配置。虽然建议使用第一种方法进行快速开发,但是第二种方法则更加灵活并且能够处理特殊情况。我们目前有2种:为JUnit测试注入模拟并从外部库中配置Bean。

In Spring we have annotation-based and XML-based configuration. While the first is recommended for quick development, the second is more flexible and able to handle special cases. By us there are currently 2: injecting mocks for JUnit tests and configuring beans from external libraries.

我没有找到与CDI的XML配置等效的任何东西,所以我的问题是是,如何处理此类bean的依赖项注入?它们来自外部库,需要进行配置,并且无法在其中添加任何注释。

I haven't found any equivalent for XML configuration for CDI, so my question is, how to handle dependency injection for such beans? They are from external libraries, they need to be configured and there's no possibility to add any annotations to them.

推荐答案

您有以下三种解决方案:

You have three solutions to your need:

CDI提供了一种转换bean中非CDI类的方法。它被称为生产者。如果要从名为 NonCdiClass 的类创建Bean,则只需创建类似的内容

CDI provides a way to transform non CDI class in beans. It is called a producer. If you want to create a bean from a class named NonCdiClass You only have to create something like that

public class MyProducers {
    @Produces
    public NonCdiClass produceNonCdiClass() {
        return new NonCdiClass();
        };
    }
  }

您现在可以 @Inject 必要时使用此bean。

You can now @Inject this bean when needed.

您可以在类中添加任意数量的生产者方法。

You can put as many producer method as you want in your class.

如果您需要在生产的bean中模拟注入,则可以通过CDI在生产者方法调用中注入参数来实现。

If you need to simulate injection in your produced bean you can do it thanks to CDI that injects parameters in producer methods calls.

@Produces
public NonCdiClass produceNonCdiClass(MyFisrtBean param1, MySecondBean param2) {
    NonCdiClass res = new NonCdiClass(param1);
    res.setParam(param2);
    return res;
    };
}

在此示例中, MyFirstBean MySecondBean 是将在生产时由CDI注入的现有bean类。

In this example MyFirstBean and MySecondBean are existing bean classes that will be injected by CDI at producing time.

生产者也可以具有限定符(在它们上或在它们的参数上)或注入 InjectionPoint 这是一个CDI内部bean,允许您根据注入位置和注解的功能来不同地生产bean。 。

Producers can also have Qualifiers (on them or on their parameters) or inject the InjectionPoint which is a CDI internal bean allowing you to produce your bean differently in function of where is the injection and what annotation it has.

InjectionPoint 示例/weld/reference/1.1.5.Final/zh-CN/html_single/#d0e1624 rel = nofollow>焊接参考文档

You have a nice InjectionPoint example in Weld reference documentation.

由于我不知道这是否是您的需要,所以在此不做详细介绍,但是您可以在AfterBeanValidation阶段的扩展中注册bean。这些注册的bean可以是您想要的任何类。
如果您需要更多信息,我可以在这里开发。

I won't go into much details here since I don't know if it's your need, but you can register bean in an extension in the AfterBeanValidation phase. These registered beans can be of any class you want. Should you need more info I could develop here.

焊接器集成了配置模块,但此项目已不再进行维护,因为已经在 Apache Deltaspike 中进行了合并。此合并在Deltaspike路线图的0.6版中: http://issues.apache.org/jira / browse / DELTASPIKE-271 。因此,您可以开始使用Solder config并在它具有此功能(应该很接近)时切换到Deltaspike。
这种解决方案不是我的最爱,但是如果您真的想在Spring上使用配置文件,那是最接近的解决方案

Solder integrated a config module but this project is no more maintained since it's been in the process to be merged in Apache Deltaspike. This merge is in Deltaspike roadmap for version 0.6: http://issues.apache.org/jira/browse/DELTASPIKE-271. So you could start using Solder config and switch to Deltaspike when it'll have the feature (which should be quite close). This solution is not my favorite but if you really want to have a config file à la Spring, it's the closest solution

这篇关于CDI:从外部库向bean注入资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆