【问题标题】:Get a CDI bean dynamically, NOT by injection [duplicate]动态获取 CDI bean,而不是通过注入 [重复]
【发布时间】:2020-08-30 09:38:41
【问题描述】:

在我的方法内部,我想获得一个 CDI bean,但我不想使用 @Inject,因为我不需要将此 bean 保存在实例级别。 我只想获取实例并将其传递给其他方法。

以前我使用 Spring,但现在切换到 JEE 应用程序。 在 Spring 在我的方法中,我可以简单地写:

MyClass myClass = applicationContext.getBean(MyClass.class)

有没有办法在 JEE 应用程序中做这样的事情?

【问题讨论】:

    标签: spring cdi java-ee-7


    【解决方案1】:

    我在这里找到了答案: How to programmatically inject a Java CDI managed bean into a local variable in a (static) method

    需要使用:

    javax.enterprise.inject.spi.CDI.current().select(C.class).get()
    

    【讨论】:

    • 有重复的不用回答。
    猜你喜欢
    • 2016-06-08
    • 2023-03-10
    • 2016-02-08
    • 1970-01-01
    • 2014-06-02
    • 2016-12-05
    • 2011-09-19
    • 2015-04-15
    • 2015-02-12
    相关资源
    最近更新 更多