Class ResteasyCdiExtension
java.lang.Object
org.jboss.resteasy.cdi.ResteasyCdiExtension
- All Implemented Interfaces:
jakarta.enterprise.inject.spi.Extension
A CDI
Extension that integrates Jakarta REST components with CDI. This extension:
- Assigns default scopes to discovered Jakarta REST resources (
RequestScoped), providers (ApplicationScoped), andApplicationsubclasses (ApplicationScoped) - Adds
@Injectto@Context-annotated fields, setter methods, and constructors so that CDI can inject context values - Registers the
@Contextand@Defaultbeans for the Jakarta REST context types - Wraps
InjectionTargetinstances for Jakarta REST components withinJaxrsInjectionTargetto handle property injection and validation - Builds the session bean interface map used by
CdiInjectorFactoryfor EJB lookup - Tracks CDI-managed Jakarta REST components in the
ResteasyBeanContainer
- Author:
- Jozef Hartinger, James R. Perkins
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclearDiscoveredTypes(jakarta.enterprise.inject.spi.AfterDeploymentValidation event) Clears the types recorded during bean discovery, which are not used once the deployment is validated.static boolean<T extends jakarta.ws.rs.core.Application>
voidobserveApplications(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points ofApplicationsubclasses and records them, so their scope and whether they are managed by CDI can be decided inobserveBeanAttributes(ProcessBeanAttributes, BeanManager).<T> voidobserveBeanAttributes(jakarta.enterprise.inject.spi.ProcessBeanAttributes<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Decides the scope of the resources, providers andApplicationsubclasses recorded when their annotated type was processed, and whether they are managed by CDI, i.e. added to the bean container.voidobserveBeforeBeanDiscovery(jakarta.enterprise.inject.spi.BeforeBeanDiscovery event) Obtain BeanManager reference for future use.<T> voidobserveInjectionTarget(jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event) Wrap InjectionTarget of JAX-RS components within JaxrsInjectionTarget which takes care of JAX-RS property injection.<T> voidobserveProviderContracts(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points of a discovered type which implements a provider contract, is not annotated with@Providerand does not declare a scope.<T> voidobserveProviders(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points of providers and records them, so their scope and whether they are managed by CDI can be decided inobserveBeanAttributes(ProcessBeanAttributes, BeanManager).<T> voidobserveResources(jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points of resources and records them, so their scope and whether they are managed by CDI can be decided inobserveBeanAttributes(ProcessBeanAttributes, BeanManager).<T> voidobserveSessionBeans(jakarta.enterprise.inject.spi.ProcessSessionBean<T> event) Observes ProcessSessionBean events and creates a (Bean class -> Local interface) map for Session beans with local interfaces.voidprocessClientBean(jakarta.enterprise.inject.spi.ProcessBean<?> pb) Process any client beans.voidregisterBeans(jakarta.enterprise.inject.spi.AfterBeanDiscovery event) Registers producers for required injections types and for aREST client.voidregisterContextProducers(jakarta.enterprise.inject.spi.AfterBeanDiscovery event, jakarta.enterprise.inject.spi.BeanManager beanManager) Registers producers for the Jakarta REST context types, theApplicationand the Jakarta Servlet types which are required to be injectable.voidvalidateContextTypeBeans(jakarta.enterprise.inject.spi.ProcessBean<?> event) Fails the deployment if a bean other than the ones registered by this extension is a@Defaultor@Contextbean for a Jakarta REST context type.voidvalidateProviderScope(jakarta.enterprise.inject.spi.ProcessManagedBean<?> event) Fails the deployment if a provider is explicitly declared@Dependent.protected <T> jakarta.enterprise.inject.spi.InjectionTarget<T> wrapInjectionTarget(jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event)
-
Constructor Details
-
ResteasyCdiExtension
public ResteasyCdiExtension()
-
-
Method Details
-
isCDIActive
public static boolean isCDIActive() -
observeBeforeBeanDiscovery
public void observeBeforeBeanDiscovery(@Observes jakarta.enterprise.inject.spi.BeforeBeanDiscovery event) Obtain BeanManager reference for future use.- Parameters:
event- event
-
processClientBean
public void processClientBean(@Observes jakarta.enterprise.inject.spi.ProcessBean<?> pb) Process any client beans.- Parameters:
pb- the bean being processed
-
validateContextTypeBeans
public void validateContextTypeBeans(@Observes jakarta.enterprise.inject.spi.ProcessBean<?> event) Fails the deployment if a bean other than the ones registered by this extension is a@Defaultor@Contextbean for a Jakarta REST context type. These would be ambiguous with the beans this extension always registers.- Parameters:
event- the bean being processed
-
validateProviderScope
public void validateProviderScope(@Observes jakarta.enterprise.inject.spi.ProcessManagedBean<?> event) Fails the deployment if a provider is explicitly declared@Dependent. Providers are application singletons, while each injection of a@Dependentbean creates a new instance. A provider which declares type variables is excluded, as CDI requires such a bean to be@Dependent. Session beans are excluded, as an unscoped session bean is made@Dependent.- Parameters:
event- the managed bean being processed
-
registerBeans
public void registerBeans(@Observes jakarta.enterprise.inject.spi.AfterBeanDiscovery event) Registers producers for required injections types and for aREST client.- Parameters:
event- the after bean discovery event
-
registerContextProducers
public void registerContextProducers(@Observes jakarta.enterprise.inject.spi.AfterBeanDiscovery event, jakarta.enterprise.inject.spi.BeanManager beanManager) Registers producers for the Jakarta REST context types, theApplicationand the Jakarta Servlet types which are required to be injectable.The Jakarta REST context types are owned by the implementation, so the
@Contextand@Defaultbeans are always registered. For theApplicationand the Servlet types, the@Defaultbean is only registered if no other bean already provides the type.- Parameters:
event- the after bean discovery eventbeanManager- the bean manager
-
clearDiscoveredTypes
public void clearDiscoveredTypes(@Observes jakarta.enterprise.inject.spi.AfterDeploymentValidation event) Clears the types recorded during bean discovery, which are not used once the deployment is validated.- Parameters:
event- the after deployment validation event
-
observeResources
public <T> void observeResources(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points of resources and records them, so their scope and whether they are managed by CDI can be decided inobserveBeanAttributes(ProcessBeanAttributes, BeanManager).- Type Parameters:
T- type- Parameters:
event- eventbeanManager- the bean manager
-
observeProviders
public <T> void observeProviders(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points of providers and records them, so their scope and whether they are managed by CDI can be decided inobserveBeanAttributes(ProcessBeanAttributes, BeanManager).- Type Parameters:
T- type- Parameters:
event- eventbeanManager- the bean manager
-
observeProviderContracts
public <T> void observeProviderContracts(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points of a discovered type which implements a provider contract, is not annotated with@Providerand does not declare a scope. Such a type is made an@ApplicationScopedprovider, or a@Singletonprovider if the type cannot be proxied, inobserveBeanAttributes(ProcessBeanAttributes, BeanManager). Providers are application singletons, while such a type would otherwise be@Dependent. This can only happen when the type is discovered without a bean defining annotation, e.g. in abean-discovery-mode="all"archive, and is registered as a provider throughApplication.getClasses()or aFeature.Only types with a
@Contextannotation are observed, as other types have no injection points to change. The scope of all such types is set inobserveBeanAttributes(ProcessBeanAttributes, BeanManager).- Type Parameters:
T- the type- Parameters:
event- the annotated type being processedbeanManager- the bean manager
-
observeBeanAttributes
public <T> void observeBeanAttributes(@Observes jakarta.enterprise.inject.spi.ProcessBeanAttributes<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Decides the scope of the resources, providers andApplicationsubclasses recorded when their annotated type was processed, and whether they are managed by CDI, i.e. added to the bean container. This is only invoked for beans, so a type which is not a bean, e.g. one with a constructor CDI cannot satisfy, is not added to the bean container and is created by RESTEasy.The
@Path,@Providerand@ApplicationPathstereotypes may define the default scopes. The same defaults are set if no scope is defined, e.g. if the annotations are not stereotypes in the environment or@Pathis only present on methods. In addition:- A resource which does not define a scope is made
@RequestScoped. An unproxyable resource which does not declare a scope is made@Dependent, which does not require a client proxy and is still created for each request. - A
@Providerwhich does not define a scope is made@ApplicationScoped. An unproxyable one which does not declare a scope is made@Singleton, which does not require a client proxy. - A provider without
@Providerwhich does not declare a scope is made@ApplicationScoped, or@Singletonif it cannot be proxied. - A resource or provider which declares type variables and does not declare a scope is made
@Dependent, as CDI requires for a bean whose type contains type variables. RESTEasy still creates a single instance of such a provider. - An
Applicationsubclass which does not declare a scope, e.g. without@ApplicationPath, is made@ApplicationScoped.
@Pathor@Providerstereotype is overridden, as the application should declare the scope.- Type Parameters:
T- the type- Parameters:
event- the bean attributes being processedbeanManager- the bean manager
- A resource which does not define a scope is made
-
observeApplications
public <T extends jakarta.ws.rs.core.Application> void observeApplications(@Observes jakarta.enterprise.inject.spi.ProcessAnnotatedType<T> event, jakarta.enterprise.inject.spi.BeanManager beanManager) Adds@Injectto the@Contextinjection points ofApplicationsubclasses and records them, so their scope and whether they are managed by CDI can be decided inobserveBeanAttributes(ProcessBeanAttributes, BeanManager). A stateless or singleton session bean is not recorded, and is made@Dependentif it does not declare a scope.- Type Parameters:
T- type- Parameters:
event- eventbeanManager- the bean manager
-
observeInjectionTarget
public <T> void observeInjectionTarget(@Observes jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event) Wrap InjectionTarget of JAX-RS components within JaxrsInjectionTarget which takes care of JAX-RS property injection.- Type Parameters:
T- type- Parameters:
event- event
-
observeSessionBeans
public <T> void observeSessionBeans(@Observes jakarta.enterprise.inject.spi.ProcessSessionBean<T> event) Observes ProcessSessionBean events and creates a (Bean class -> Local interface) map for Session beans with local interfaces. This map is necessary since RESTEasy identifies a bean class as JAX-RS components while CDI requires a local interface to be used for lookup.- Type Parameters:
T- type- Parameters:
event- event
-
getSessionBeanInterface
-
wrapInjectionTarget
protected <T> jakarta.enterprise.inject.spi.InjectionTarget<T> wrapInjectionTarget(jakarta.enterprise.inject.spi.ProcessInjectionTarget<T> event)
-