@Vetoed public class Weld extends Object implements ContainerInstanceFactory
This builder is a preferred method of booting Weld SE container.
Typical usage looks like this:
WeldContainer container = new Weld().initialize(); container.select(Foo.class).get(); container.event().select(Bar.class).fire(new Bar()); container.shutdown();
The WeldContainer
implements AutoCloseable:
try (WeldContainer container = new Weld().initialize()) { container.select(Foo.class).get(); }
By default, the discovery is enabled so that all beans from all discovered bean archives are considered. However, it's possible to define a "synthetic" bean archive, or the set of bean classes and enablement respectively:
WeldContainer container = new Weld().beanClasses(Foo.class, Bar.class).alternatives(Bar.class).initialize()) {
Moreover, it's also possible to disable the discovery completely so that only the "synthetic" bean archive is considered:
WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bar.class).initialize()) {
In the same manner, it is possible to explicitly declare interceptors, decorators, extensions and Weld-specific options (such as relaxed construction) using the builder.
Weld builder = new Weld() .disableDiscovery() .packages(Main.class, Utils.class) .interceptors(TransactionalInterceptor.class) .property("org.jboss.weld.construction.relaxed", true); WeldContainer container = builder.initialize();
The builder is reusable which means that it's possible to initialize multiple Weld containers with one builder. However, note that containers must have a unique identifier assigned when running multiple Weld instances at the same time.
WeldContainer
Modifier and Type | Field and Description |
---|---|
static String |
ARCHIVE_ISOLATION_SYSTEM_PROPERTY |
static String |
DEV_MODE_SYSTEM_PROPERTY |
static String |
SHUTDOWN_HOOK_SYSTEM_PROPERTY |
Modifier and Type | Method and Description |
---|---|
Weld |
addAlternative(Class<?> alternativeClass)
Add an alternative class to the list of selected alternatives for a synthetic bean archive.
|
Weld |
addAlternativeStereotype(Class<? extends Annotation> alternativeStereotypeClass)
Add an alternative stereotype class to the list of selected alternative stereotypes for a synthetic bean archive.
|
Weld |
addBeanClass(Class<?> beanClass)
Add a bean class to the set of bean classes for the synthetic bean archive.
|
Weld |
addDecorator(Class<?> decoratorClass)
Add a decorator class to the list of enabled decorators for a synthetic bean archive.
|
Weld |
addExtension(Extension extension)
Add an extension to the set of extensions.
|
Weld |
addInterceptor(Class<?> interceptorClass)
Add an interceptor class to the list of enabled interceptors for a synthetic bean archive.
|
Weld |
addPackage(boolean scanRecursively,
Class<?> packageClass)
A package of the specified class will be scanned and found classes will be added to the set of bean classes for the synthetic bean archive.
|
Weld |
addPackages(boolean scanRecursively,
Class<?>... packageClasses)
Packages of the specified classes will be scanned and found classes will be added to the set of bean classes for the synthetic bean archive.
|
Weld |
alternatives(Class<?>... alternativeClasses)
Select alternatives for a synthetic bean archive.
|
Weld |
alternativeStereotypes(Class<? extends Annotation>... alternativeStereotypeClasses)
Select alternative stereotypes for a synthetic bean archive.
|
Weld |
beanClasses(Class<?>... classes)
Define the set of bean classes for the synthetic bean archive.
|
Weld |
containerId(String containerId)
Containers must have a unique identifier assigned when running multiple Weld instances at the same time.
|
protected Deployment |
createDeployment(ResourceLoader resourceLoader,
CDI11Bootstrap bootstrap)
Extensions to Weld SE can subclass and override this method to customize the deployment before weld boots up.
|
Weld |
decorators(Class<?>... decoratorClasses)
Enable decorators for a synthetic bean archive.
|
Weld |
disableDiscovery()
By default, the discovery is enabled.
|
Weld |
enableDiscovery() |
Weld |
extensions(Extension... extensions)
Define the set of extensions.
|
String |
getContainerId() |
protected <T> T |
getInstanceByType(BeanManager manager,
Class<T> type,
Annotation... bindings)
Utility method allowing managed instances of beans to provide entry points for non-managed beans (such as
WeldContainer ). |
WeldContainer |
initialize()
Bootstraps a new Weld SE container with the current
containerId . |
Weld |
interceptors(Class<?>... interceptorClasses)
Enable interceptors for a synthetic bean archive.
|
boolean |
isDiscoveryEnabled() |
Weld |
packages(Class<?>... packageClasses)
All classes from the packages of the specified classes will be added to the set of bean classes for the synthetic bean archive.
|
Weld |
property(String key,
Object value)
Set the configuration property.
|
Weld |
reset()
Reset the synthetic bean archive (bean classes and enablement) and explicitly added extensions.
|
Weld |
resetAll()
Reset all the state, except for initialized containers.
|
Weld |
setClassLoader(ClassLoader classLoader)
Set a
ClassLoader . |
Weld |
setResourceLoader(ResourceLoader resourceLoader)
Set a
ResourceLoader used to scan the application for bean archives. |
void |
shutdown()
Shuts down all the containers initialized by this builder.
|
public static final String ARCHIVE_ISOLATION_SYSTEM_PROPERTY
public static final String DEV_MODE_SYSTEM_PROPERTY
public static final String SHUTDOWN_HOOK_SYSTEM_PROPERTY
public Weld()
public Weld(String containerId)
containerId
- The container identifiercontainerId(String)
public Weld containerId(String containerId)
containerId
- public String getContainerId()
containerId(String)
public Weld beanClasses(Class<?>... classes)
classes
- public Weld addBeanClass(Class<?> beanClass)
beanClass
- public Weld packages(Class<?>... packageClasses)
Note that the scanning possibilities are limited. Therefore, only directories and jar files from the filesystem are supported.
Scanning may also have negative impact on bootstrap performance.
classes
- public Weld addPackages(boolean scanRecursively, Class<?>... packageClasses)
scanRecursively
- packageClasses
- public Weld addPackage(boolean scanRecursively, Class<?> packageClass)
scanRecursively
- packageClass
- public Weld extensions(Extension... extensions)
extensions
- public Weld addExtension(Extension extension)
extension
- an extensionpublic Weld interceptors(Class<?>... interceptorClasses)
interceptorClasses
- public Weld addInterceptor(Class<?> interceptorClass)
interceptorClass
- public Weld decorators(Class<?>... decoratorClasses)
decoratorClasses
- public Weld addDecorator(Class<?> decoratorClass)
decoratorClass
- public Weld alternatives(Class<?>... alternativeClasses)
alternativeClasses
- public Weld addAlternative(Class<?> alternativeClass)
alternativeClass
- @SafeVarargs public final Weld alternativeStereotypes(Class<? extends Annotation>... alternativeStereotypeClasses)
alternativeStereotypeClasses
- public Weld addAlternativeStereotype(Class<? extends Annotation> alternativeStereotypeClass)
alternativeStereotypeClass
- public Weld property(String key, Object value)
key
- value
- public Weld reset()
public Weld resetAll()
reset()
public Weld enableDiscovery()
disableDiscovery()
public Weld disableDiscovery()
public boolean isDiscoveryEnabled()
true
if the discovery is enabled, false
otherwisedisableDiscovery()
public WeldContainer initialize()
containerId
.
The container must be shut down properly when an application is stopped. Applications are encouraged to use the try-with-resources statement or invoke
WeldContainer.shutdown()
explicitly.
However, a shutdown hook is also registered during initialization so that all running containers are shut down automatically when a program exits or VM
is terminated. This means that it's not necessary to implement the shutdown logic in a class where a main method is used to start the container.initialize
in interface ContainerInstanceFactory
enableDiscovery()
,
WeldContainer.shutdown()
public void shutdown()
public Weld setClassLoader(ClassLoader classLoader)
ClassLoader
. The given ClassLoader
will be scanned automatically for bean archives if scanning is enabled.classLoader
- public Weld setResourceLoader(ResourceLoader resourceLoader)
ResourceLoader
used to scan the application for bean archives. If you only want to use a specific ClassLoader
for scanning, use
setClassLoader(ClassLoader)
instead.resourceLoader
- isDiscoveryEnabled()
protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap)
Extensions to Weld SE can subclass and override this method to customize the deployment before weld boots up. For example, to add a custom ResourceLoader, you would subclass Weld like so:
public class MyWeld extends Weld { protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) { return super.createDeployment(new MyResourceLoader(), bootstrap); } }
This could then be used as normal:
WeldContainer container = new MyWeld().initialize();
resourceLoader
- bootstrap
- protected <T> T getInstanceByType(BeanManager manager, Class<T> type, Annotation... bindings)
WeldContainer
). Should only called once
Weld has finished booting.manager
- the BeanManager to use to access the managed instancetype
- the type of the Beanbindings
- the bean's qualifiersIllegalArgumentException
- if the given type represents a type variableIllegalArgumentException
- if two instances of the same qualifier type are givenIllegalArgumentException
- if an instance of an annotation that is not a qualifier type is givenUnsatisfiedResolutionException
- if no beans can be resolved * @throws AmbiguousResolutionException if the ambiguous dependency resolution rules
failIllegalArgumentException
- if the given type is not a bean type of the given beanCopyright © 2015. All Rights Reserved.