GlassFish,CDI和构造器注射 [英] GlassFish, CDI and constructor injection

查看:142
本文介绍了GlassFish,CDI和构造器注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GlassFish 3.1中是否支持构造函数注入管理bean的CDI?我有一个 @Singleton EJB,我想使用构造函数注入来注入另一个托管的bean(包含在同一个EJB模块中)。现场注射工作。但是使用构造函数注入,我从 AbstractSingletonContainer 中得到一个 NullPointerException

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.

这样做:

@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支持直接现场注入,初始化方法参数注入和构造函数参数注入。从CDI 1.0规范:

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


3.7。 Bean构造函数



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

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.

应用程序可以直接调用bean
构造函数。但是,如果
应用程序直接实例化
bean,则不会通过容器将参数传递给
构造函数;
返回的对象不绑定到任何
上下文;没有依赖关系由容器注入
;并且
的生命周期新的实例不由
容器管理。

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.

可以通过注释构造函数
@Inject c>。

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() {}

    ...
}

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

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

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

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

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

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

一个bean构造函数可能有任何数字
的参数。
bean构造函数的所有参数都是注入点。

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

我想知道你的问题是否可以与 WELD-141

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


  • CDI 1.0规范


    • 第3.7节。 Bean构造函数

    • 4.1. Injection points

    这篇关于GlassFish,CDI和构造器注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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