Weld 3.0.0.Alpha5

2015-2-25   release , cdi2   Jozef Hartinger

Weld 3.0.0.Alpha5, the latest release in the series of CDI 2.0 prototypes, has been released. The highlights of the release include new Bean builder API and many improvements to Weld Probe.

Bean Builder API

The common way for frameworks and libraries to expose their API to applications via CDI is by using producer methods. An alternative approach is a custom implementation of the javax.enterprise.inject.spi.Bean interface that is registered by a CDI portable extension. This latter approach gives the extension more control but is also less straightforward.

One of the goals of CDI 2.0 is to fill this gap and simplify extension development. This Weld release comes with a preview of the Bean builder API. The API is inspired by Bean builder API provided by DeltaSpike. In addition, Java 8 lambdas are used in the API to simplify the entire process even further.

Here’s an example of a simple extension that builds and registers a bean:

void register(@Observes ExperimentalAfterBeanDiscovery event) {
    event.addBean()
        .addType(Integer.class)
        .addQualifier(Random.LITERAL)
        .produceWith(() -> new java.util.Random().nextInt(1000));
}

That’s it! Likewise, it is easy to can define a bean with custom disposal logic using a lambda expression:

void register(@Observes ExperimentalAfterBeanDiscovery event) {
    event.addBean()
        .addType(Connection.class)
        .produceWith(this::newConnection)
        .disposeWith(connection -> connection.close());
}

See the BeanBuilder Javadoc for more information.

Event monitoring with Probe

We introduced Probe in the previous blog post. This release adds event monitoring view to Probe. In this view both system and application events are displayed. The events can be filtered by type and qualifiers. It is also possible to track which observer methods a particular event was delivered to.

Weld Probe event monitoring

Further improvements

In addition, there were a couple of minor improvements to Probe, namely:

  • better filtering of invocation trees - Probe now searches the whole tree, not just the entry points

  • the context view now displays the conversation context

  • Probe is now more careful and does not try to bind interceptors to classes that are not proxyable

  • all the configuration options for Probe are now documented

If you haven’t tried Probe yet check this demo running on OpenShift. The easiest way to get Probe to run locally is to patch WildFly with a Weld patch. If you’re not familiar with patching WildFly, check Markus’s tutorial.

Relaxed construction

CDI requires that beans that are normal-scoped, intercepted or decorated always define a no-argument constructor. This requirement applies even if the bean already defines an @Inject annotated constructor with parameters. This is purely a technical requirement implied by how Java allocates class instances.

Weld is however able to operate fine even if this requirement is not met. Weld uses special non-portable JVM APIs that allow it to allocate proxy instances without calling proxy’s constructor. This mode is non-portable and needs to be explicitly enabled using a configuration option.

This mode is not new but has been neglected in the past. For this release we added a bunch of tests for this mode, fixed a lot of bugs and enabled it by default in Weld-SE. Any feedback on this mode is appreciated.