CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-quarkus--quarkus-bom

Cloud Native, Container First Java framework for building efficient Java applications with fast startup times and low memory usage

Pending
Overview
Eval results
Files

cdi-dependency-injection.mddocs/

CDI and Dependency Injection

Quarkus uses ArC, a build-time optimized CDI container that provides dependency injection, context management, and lifecycle handling with full native image support.

ArC Container Access

Arc Main Class

public final class Arc {
    public static ArcContainer initialize();
    public static ArcContainer initialize(ArcInitConfig config);
    public static void setExecutor(ExecutorService executor);
    public static ArcContainer container();
    public static void shutdown();
}

Main access point to the ArC CDI container. Use container() to access container services programmatically.

Usage Example:

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;

// Get a bean instance programmatically
InstanceHandle<MyService> handle = Arc.container().instance(MyService.class);
MyService service = handle.get();

// Use the service
service.doSomething();

// Clean up when done
handle.destroy();

ArcContainer Interface

public interface ArcContainer {
    // Bean instance access
    <T> InstanceHandle<T> instance(Class<T> type, Annotation... qualifiers);
    <T> InstanceHandle<T> instance(TypeLiteral<T> type, Annotation... qualifiers);
    <X> InstanceHandle<X> instance(Type type, Annotation... qualifiers);
    <T> InstanceHandle<T> instance(String name);
    <T> InstanceHandle<T> instance(InjectableBean<T> bean);
    
    // Programmatic selection
    <T> InjectableInstance<T> select(Class<T> type, Annotation... qualifiers);
    <T> InjectableInstance<T> select(TypeLiteral<T> type, Annotation... qualifiers);
    
    // Bean listing and suppliers
    <T> List<InstanceHandle<T>> listAll(Class<T> type, Annotation... qualifiers);
    <T> List<InstanceHandle<T>> listAll(TypeLiteral<T> type, Annotation... qualifiers);
    <X> List<InstanceHandle<X>> listAll(Type type, Annotation... qualifiers);
    <T> Supplier<InstanceHandle<T>> beanInstanceSupplier(Class<T> type, Annotation... qualifiers);
    
    // Bean access
    <T> InjectableBean<T> bean(String beanIdentifier);
    InjectableBean<?> namedBean(String name);
    
    // Context management
    InjectableContext getActiveContext(Class<? extends Annotation> scopeType);
    List<InjectableContext> getContexts(Class<? extends Annotation> scopeType);
    Set<Class<? extends Annotation>> getScopes();
    ManagedContext requestContext();
    ManagedContext sessionContext();
    
    // Container state and services
    boolean isRunning();
    BeanManager beanManager();
    ExecutorService getExecutorService();
    CurrentContextFactory getCurrentContextFactory();
    boolean strictCompatibility();
    
    // Event observers
    <T> List<InjectableObserverMethod<? super T>> resolveObserverMethods(Type eventType, Annotation... eventQualifiers);
}

The main container interface providing bean access and lifecycle management.

InstanceHandle Interface

public interface InstanceHandle<T> extends AutoCloseable, Instance.Handle<T> {
    T get();
    default boolean isAvailable();
    default T orElse(T other);
    default void destroy();
    default InjectableBean<T> getBean();
    @Override
    default void close();
}

Handle for managed bean instances with automatic lifecycle management.

InjectableInstance Interface

public interface InjectableInstance<T> extends Instance<T> {
    @Override
    InjectableInstance<T> select(Annotation... qualifiers);
    @Override
    <U extends T> InjectableInstance<U> select(Class<U> subtype, Annotation... qualifiers);
    @Override
    <U extends T> InjectableInstance<U> select(TypeLiteral<U> subtype, Annotation... qualifiers);
    
    @Override
    InstanceHandle<T> getHandle();
    @Override
    Iterable<InstanceHandle<T>> handles();
    @Override
    default Stream<InstanceHandle<T>> handlesStream();
    
    void clearCache();
    @Override
    Iterator<T> iterator();
    default T orElse(T other);
    default T orNull();
    default T getActive();
    default List<T> listActive();
}

CDI Instance wrapper providing programmatic bean access with ArC-specific enhancements.

Standard CDI Annotations

Scope Annotations

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Scope
public @interface ApplicationScoped {
}

Application-scoped beans (singleton per application).

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Scope
public @interface RequestScoped {
}

Request-scoped beans (one instance per request).

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Scope
public @interface SessionScoped {
}

Session-scoped beans (one instance per HTTP session).

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Scope
public @interface Singleton {
}

Singleton-scoped beans (eager instantiation).

Usage Example:

@ApplicationScoped
public class UserService {
    
    @Inject 
    UserRepository repository;
    
    public User findById(Long id) {
        return repository.findById(id);
    }
}

Injection Annotations

@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}

Standard CDI injection annotation.

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Named {
    String value() default "";
}

Named qualifier for disambiguation.

ArC-Specific Extensions

Bean Configuration Annotations

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DefaultBean {
}

Marks beans as default implementations that can be overridden by other beans.

Usage Example:

@DefaultBean
@ApplicationScoped
public class DefaultEmailService implements EmailService {
    // Default implementation
}

@ApplicationScoped  // This will override the default
public class CustomEmailService implements EmailService {
    // Custom implementation
}
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Unremovable {
}

Prevents bean removal during build-time optimization.

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface All {
}

Qualifier for injecting all instances of a particular type.

Usage Example:

@ApplicationScoped
public class NotificationService {
    
    @Inject
    @All
    List<NotificationHandler> handlers;  // Injects all NotificationHandler implementations
    
    public void notify(String message) {
        handlers.forEach(handler -> handler.handle(message));
    }
}

Producer Configuration

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface VetoedProducer {
}

Excludes producer methods from CDI processing.

Programmatic Bean Management

BeanCreator

public abstract class BeanCreator<T> {
    public static <T> BeanCreator<T> create(Class<T> beanClass);
    public BeanCreator<T> scope(Class<? extends Annotation> scope);
    public BeanCreator<T> addQualifier(Annotation qualifier);
    public BeanCreator<T> creator(CreationalContext<T> creationalContext);
    public void done();
}

Programmatic API for creating beans at runtime.

BeanDestroyer

public interface BeanDestroyer<T> {
    void destroy(T instance, CreationalContext<T> creationalContext);
}

Interface for custom bean destruction logic.

Context Management

ManagedContext

public interface ManagedContext extends InjectableContext {
    void activate();
    void deactivate();
    void terminate();
    boolean isActive();
}

Interface for programmatically managed contexts.

Usage Example:

@ApplicationScoped
public class RequestContextService {
    
    @Inject
    ManagedContext requestContext;
    
    public void executeInRequestContext(Runnable task) {
        requestContext.activate();
        try {
            task.run();
        } finally {
            requestContext.terminate();
        }
    }
}

InjectableContext Interface

public interface InjectableContext extends Context {
    void destroy();
    void destroy(Contextual<?> contextual);
    ContextState getState();
}

Enhanced context interface with destruction capabilities.

CurrentContext

public class CurrentContext {
    public static boolean isActive(Class<? extends Annotation> scopeType);
    public static <T> T get(Class<T> beanClass);
    public static <T> T get(Class<T> beanClass, Annotation... qualifiers);
}

Utility class for accessing current context state.

Event System

Event Production and Observation

@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Observes {
    Reception notifyObserver() default Reception.ALWAYS;
    TransactionPhase during() default TransactionPhase.IN_PROGRESS;
}

Standard CDI event observation annotation.

public interface Event<T> {
    void fire(T event);
    <U extends T> void fireAsync(U event);
    Event<T> select(Annotation... qualifiers);
}

Interface for firing CDI events.

Usage Example:

@ApplicationScoped
public class OrderService {
    
    @Inject
    Event<OrderPlaced> orderEvent;
    
    public void placeOrder(Order order) {
        // Process order
        orderEvent.fire(new OrderPlaced(order));
    }
}

@ApplicationScoped 
public class OrderEventHandler {
    
    void onOrderPlaced(@Observes OrderPlaced event) {
        System.out.println("Order placed: " + event.getOrder().getId());
    }
}

Interceptors and Decorators

Interceptor Annotations

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public @interface Transactional {
    TxType value() default TxType.REQUIRED;
    Class[] rollbackOn() default {};
    Class[] dontRollbackOn() default {};
}

Transaction interceptor binding (example of interceptor usage).

ArC Priority Constants

public final class InvocationContextImpl {
    public static final int PLATFORM_BEFORE = 0;
    public static final int LIBRARY_BEFORE = 1000;
    public static final int APPLICATION = 2000;
    public static final int LIBRARY_AFTER = 3000;
    public static final int PLATFORM_AFTER = 4000;
}

Priority constants for ordering interceptors and observers.

Configuration Integration

ConfigProperties Integration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigProperties {
    String prefix() default "";
    NamingStrategy namingStrategy() default NamingStrategy.KEBAB_CASE;
}

Integration between CDI and MicroProfile Config for configuration properties binding.

Usage Example:

@ConfigProperties(prefix = "app.database")
@ApplicationScoped
public class DatabaseConfig {
    public String host;
    public int port;
    public String username;
    public String password;
}

Install with Tessl CLI

npx tessl i tessl/maven-io-quarkus--quarkus-bom

docs

cdi-dependency-injection.md

configuration.md

core-runtime.md

data-persistence.md

index.md

reactive-programming.md

rest-web-services.md

scheduling.md

security.md

testing.md

tile.json