GlassFish、CDI 和构造函数注入

发布于 2024-09-15 15:31:23 字数 537 浏览 3 评论 0原文

GlassFish 3.1 的托管 bean 的 CDI 实现是否支持构造函数注入?我有一个 @Singleton EJB,我想使用构造函数注入向其中注入另一个托管 Bean(包含在同一 EJB 模块中)。现场注入确实有效。但是通过构造函数注入,我从 AbstractSingletonContainer 得到一个 NullPointerException

这有效:

@Singleton
public class FooBean implements Foo {

  @Inject private BarBean bar;

}

这不起作用:

@Singleton
public class FooBean implements Foo {

    private final BarBean bar;

    @Inject
    public FooBean(BarBean bar) {
        this.bar = bar;
    }

}

Is constructor injection supported in GlassFish 3.1's implementation of CDI for managed beans? I have a @Singleton EJB into which I want to inject another managed bean (contained in the same EJB module) using constructor injection. Field injection does work. But with constructor injection I get a NullPointerException from AbstractSingletonContainer.

This does work:

@Singleton
public class FooBean implements Foo {

  @Inject private BarBean bar;

}

This does not work:

@Singleton
public class FooBean implements Foo {

    private final BarBean bar;

    @Inject
    public FooBean(BarBean bar) {
        this.bar = bar;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳钉梦 2024-09-22 15:31:23

CDI 支持直接字段注入、初始化方法参数注入和构造函数参数注入。来自CDI 1.0规范:

3.7。 Bean 构造函数

当容器实例化一个bean时
类,它调用 bean
构造函数。
。 bean 构造函数是
bean 类的构造函数。

应用程序可以调用bean
直接构造函数。然而,如果
应用程序直接实例化
bean,没有参数传递给
由容器构造函数;这
返回的对象未绑定到任何
语境;没有注入任何依赖项
通过容器;和生命周期
新实例不受管理
容器。

3.7.1。声明 bean 构造函数

bean 构造函数可以被识别
通过注释构造函数
@Inject

<前><代码>@SessionScoped
公共类 ShoppingCart 实现可序列化 {
私人用户客户;

@注入
公共购物车(用户客户){
this.customer = 客户;
}

公共购物车(购物车原创){
this.customer = 原始.customer;
}

购物车() {}

...
}

@ConversationScoped
公开课顺序{
私人产品产品;
私人用户客户;

@注入
公共订单(@选定产品产品,用户客户){
这个产品=产品;
this.customer = 客户;
}

公共订单(订单原件){
this.product = 原始.product;
this.customer = 原始.customer;
}

命令() {}

...
}

如果一个bean类没有显式地
使用@Inject声明构造函数,
不接受 no 的构造函数
参数是bean构造函数。

如果一个 Bean 类有多个
构造函数注释为@Inject
容器自动检测
问题并将其视为定义
错误。

如果 bean 构造函数有参数
带注释的 @Disposes@Observes
容器自动检测
问题并将其视为
定义错误。

bean 构造函数可以有任意数量
参数。 a的所有参数
bean 构造函数是注入点。

我想知道您的问题是否可能与 WELD-141 有关。

参考

  • CDI 1.0规范
    • 第 3.7 节。 “Bean 构造函数”
  • Weld 文档

CDI does support direct field injection, initializer method parameter injection and constructor parameter injection. From the CDI 1.0 specification:

3.7. Bean constructors

When the container instantiates a bean
class, it calls the bean
constructor
. The bean constructor is
a constructor of the bean class.

The application may call bean
constructors directly. However, if the
application directly instantiates the
bean, no parameters are passed to the
constructor by the container; the
returned object is not bound to any
context; no dependencies are injected
by the container; and the lifecycle of
the new instance is not managed by the
container.

3.7.1. Declaring a bean constructor

The bean constructor may be identified
by annotating the constructor
@Inject.

@SessionScoped
public class ShoppingCart implements Serializable {
    private User customer;

    @Inject
    public ShoppingCart(User customer) {
        this.customer = customer;
    }

    public ShoppingCart(ShoppingCart original) {
        this.customer = original.customer;
    }

    ShoppingCart() {}

    ...
}

@ConversationScoped
public class Order {
    private Product product;
    private User customer;

    @Inject
    public Order(@Selected Product product, User customer) {
        this.product = product;
        this.customer = customer;
    }

    public Order(Order original) {
        this.product = original.product;
        this.customer = original.customer;
    }

    Order() {}

    ...
}

If a bean class does not explicitly
declare a constructor using @Inject,
the constructor that accepts no
parameters is the bean constructor.

If a bean class has more than one
constructor annotated @Inject, the
container automatically detects the
problem and treats it as a definition
error.

If a bean constructor has a parameter
annotated @Disposes, or @Observes,
the container automatically detects
the problem and treats it as a
definition error.

A bean constructor may have any number
of parameters. All parameters of a
bean constructor are injection points.

I wonder if your problem could be related to WELD-141 though.

References

迷爱 2024-09-22 15:31:23

GlassFish 3.x 支持构造函数注入,但您必须提供默认构造函数才能满足 EJB 规范。

这将起作用:

@Singleton
public class FooBean implements Foo {

    private final BarBean bar;

    public FooBean() {
      this.bar = null;
    }

    @Inject
    public FooBean(BarBean bar) {
        this.bar = bar;
    } 
}

但是 Glassfish(这部分取决于容器)将在注入的构造函数之前调用默认构造函数。

Constructor injection is supported in GlassFish 3.x but you must provide a default constructor anyway to satisfy EJB specs.

This will work:

@Singleton
public class FooBean implements Foo {

    private final BarBean bar;

    public FooBean() {
      this.bar = null;
    }

    @Inject
    public FooBean(BarBean bar) {
        this.bar = bar;
    } 
}

but Glassfish (this part is container dependant) will call the default constructor before the Injected one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文