dependency-injection - GlassFish、CDI 和构造函数注入(inject)

标签 dependency-injection glassfish java-ee-6 cdi constructor-injection

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

这确实有效:

@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;
    }

}

最佳答案

CDI 确实支持直接字段注入(inject)、初始化方法参数注入(inject)和构造函数参数注入(inject)。从 CDI 1.0 规范:

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.



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

引用
  • CDI 1.0 规范
  • 第 3.7 节。 “Bean 构造函数”
  • 焊接文件
  • 4.1. Injection points
  • 关于dependency-injection - GlassFish、CDI 和构造函数注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3541448/

    相关文章:

    c# - 当我的复合根引用数据层时,我的 DI 架构是否正确?

    java - Glassfish/Java EE6/MySQL 中的特殊字符问题

    jakarta-ee - 在 JAX-RS 中使用 @stateless 在 Java EE 6 及更高版本中提供什么好处?

    timer - 如何在每个月底运行的 JavaEE6 中测试定时器服务 (EJBTimer)

    dependency-injection - 您最常使用哪种松散耦合模式?

    angularjs - TypeScript 构造函数中的私有(private)变量声明以引发 DI

    java - 我应该为我的项目选择哪个应用服务器?

    ssl - 使用客户端证书的 GlassFish Metro 出站 Web 服务调用失败

    javascript - 如何使用JavaScript将文件作为多部分文件上传到后端?

    angularjs - Angular 依赖注入(inject) - 我一直看到两种不同的方法