Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page

Was this helpful?

  1. CDI 2.0

Java SE support

The Java SE support in weld is now standardized, it is useful when you want to get CDI support out of Java EE application servers.

Given a simple CDI bean.

@Named
@ApplicationScoped
public class Greeter {

    public void say(String name) {
        System.out.println("Hi, " + name);
    }
}

You can start CDI SeContainer like this.

 SeContainerInitializer initializer = SeContainerInitializer.newInstance();
try (SeContainer container = initializer.initialize()) {
    assertTrue(container.isRunning());
    Set<Bean<?>> greeters = container.getBeanManager().getBeans("greeter");
    assertTrue(greeters.size() == 1);
}

There is a try-resources statement, SeContainer is a AutoClosable and can be closed automatically at the end.

To bootstrap CDI container for Java SE, you have to add the following dependencies in project.

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-shaded</artifactId>
    <version>3.0.0.Final</version>
</dependency>

Weld also provides some simple APIs to bootstrap a CDI container.

Weld weld = new Weld();

try (WeldContainer container = weld.initialize()) {
    Greeter greeter = container.select(Greeter.class).get();
    assertTrue(greeter != null);
}

You can utilize Weld APIs to modify beans in the CDI container lifecycle.

@Test(expected = UnsatisfiedResolutionException.class)
public void bootWeldSeContainer() {
    Extension testExtension = ContainerLifecycleObserver.extensionBuilder()
        .add(afterBeanDiscovery((e) -> System.out.println("Bean discovery completed!")))
        .add(processAnnotatedType().notify((e) -> {
            if (e.getAnnotatedType().getJavaClass().getName().startsWith("com.hantsylab")) {
                e.veto();
            }
        }))
        .build();
    try (WeldContainer container = new Weld().addExtension(testExtension).initialize()) {
        Greeter greeter = container.select(Greeter.class).get();
        //assertTrue(greeter == null);
    }
}

In the above codes, it vetoes Greeter bean in processAnnotatedType phase, the later bean select operation will cause an exception UnsatisfiedResolutionException thrown.

PreviousCDI 2.0NextEvent Priority

Last updated 4 years ago

Was this helpful?

Grab the from my GitHub account, and have a try.

source codes