Glassfish, CDI and constructor injection

Is constructor installation supported in GlassFish 3.1 CDI implementation for managed beans? I have an @SingletonEJB in which I want to add another managed bean (contained in the same EJB module) using constructor injection. Field injection really works. But with the constructor injector I get a NullPointerExceptionfrom AbstractSingletonContainer.

It works:

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

}
+5
source share
2 answers

CDI supports direct field injection, parameter input of the initializer method, and constructor parameter injector. From the CDI 1.0 specification:

3.7. Bean Constructors

bean, bean. Bean Bean.

bean . , bean, ; - ; ; .

3.7.1. Bean

Bean @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.

Bean @Inject, .

Bean @Disposes @Observes, .

Bean . Bean - .

, WELD-141, .

  • CDI 1.0
    • 3.7. "Bean "
+9

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 ( ) Injected.

+3

All Articles