Weld has provided support for the Java SE environment for a long time with the weld-se module.
The API provides an easy way for an application to initialize Weld and use it in a standalone mode.
On initialization Weld SE scans the classpath for bean archives with the beans.xml file, similarly to how it’s done in the Java EE environment.
In this release we are extending the API further.
This is partially inspired by the current discussion in the CDI expert group where a standardized CDI API for Java SE is being proposed as part of CDI-26.
The following code snippet shows the new API in action:
Weld builder = new Weld()
.disableDiscovery()
.packages(Main.class, Utils.class)
.interceptors(TransactionalInterceptor.class)
.property("org.jboss.weld.construction.relaxed", true);
try (WeldContainer weld = builder.initialize()) {
MyBean bean = weld.select(MyBean.class).get();
System.out.println(bean.computeResult());
}
There are several new things to notice:
-
the Weld class is used as a builder to configure Weld before it is initialized
-
automatic scanning can be disabled
-
instead of scanning, classes or packages can be selected explicitly. All classes in those packages will be managed by Weld
-
interceptors, decorators, extensions and Weld-specific configuration options can be specified using the builder
-
WeldContainer now implements AutoCloseable and can therefore be used in a try-with-resources block. At any time that execution gets outside of the code block, the Weld instance is shut down and all managed instances are safely destroyed.
It is also possible to start multiple independent Weld instances:
new Weld().disableDiscovery().containerId("one").beanClasses(MyBean.class).initialize();
new Weld().disableDiscovery().containerId("two").beanClasses(OtherBean.class).initialize();
MyBean bean = WeldContainer.instance("one").select(MyBean.class).get();
System.out.println(bean.computeResult());
WeldContainer.instance("one").shutdown();
WeldContainer.instance("two").shutdown();
Here, two independent WeldContainer instances are initialized.
Each of them is given a unique ID.
The ID can subsequently be used to obtain a WeldContainer reference in a different place of the code.
One possible use-case this enables is for a library or framework (e.g. a testing framework) to use an embedded instance of Weld internally for its own needs (dependency injection, events, extensibility).
This instance would not interfere with the Weld instance used by the application.
Obviously, automatic classpath scanning can still be used as before:
try (WeldContainer weld = new Weld().enableDiscovery().initialize()) {
MyBean bean = weld.select(MyBean.class).get();
System.out.println(bean.computeResult());
}
To play with the new API use the following dependency in you Maven project:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.0.Alpha8</version>
</dependency>
Aforementioned classes are from the org.jboss.weld.environment.se package.