Package io.micronaut.context.scope
Class AbstractConcurrentCustomScope<A extends Annotation>
java.lang.Object
io.micronaut.context.scope.AbstractConcurrentCustomScope<A>
- Type Parameters:
A- The annotation type
- All Implemented Interfaces:
LifeCycle<AbstractConcurrentCustomScope<A>>,CustomScope<A>,Closeable,AutoCloseable
public abstract class AbstractConcurrentCustomScope<A extends Annotation>
extends Object
implements CustomScope<A>, LifeCycle<AbstractConcurrentCustomScope<A>>, AutoCloseable
Abstract implementation of the custom scope interface that simplifies defining new scopes using the Map interface.
Note this implementation uses a singleReentrantReadWriteLock to lock the entire scope hence it is designed for scopes that will hold a small amount of beans. For implementations that hold many beans it is recommended to use a lock per BeanIdentifier.
- Since:
- 3.0.0
- Author:
- graemerocher
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractConcurrentCustomScope(Class<A> annotationType) A custom scope annotation. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose()Implement the close logic for the scope.protected voiddestroyScope(@Nullable Map<BeanIdentifier, CreatedBean<?>> scopeMap) Destroys the scope.protected <T> @NonNull CreatedBean<T>doCreate(@NonNull BeanCreationContext<T> creationContext) Perform creation.final <T> Optional<BeanRegistration<T>>findBeanRegistration(T bean) Get theBeanDefinitionfor the given bean.final <T> TgetOrCreate(BeanCreationContext<T> creationContext) Resolve an object from the given scope.protected abstract @NonNull Map<BeanIdentifier,CreatedBean<?>> getScopeMap(boolean forCreation) protected voidMethod that can be overridden to customize what happens on a shutdown error.final <T> Optional<T>remove(BeanIdentifier identifier) Remove a bean definition from the scope.stop()Stops the life cycle component.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.context.scope.CustomScope
findBeanRegistration
-
Constructor Details
-
AbstractConcurrentCustomScope
A custom scope annotation.- Parameters:
annotationType- The annotation type
-
-
Method Details
-
getScopeMap
@NonNull protected abstract @NonNull Map<BeanIdentifier,CreatedBean<?>> getScopeMap(boolean forCreation) - Parameters:
forCreation- Whether it is for creation- Returns:
- Obtains the scope map, never null
- Throws:
IllegalStateException- if the scope map cannot be obtained in the current context
-
annotationType
- Specified by:
annotationTypein interfaceCustomScope<A extends Annotation>- Returns:
- The scope annotation type
-
close
public abstract void close()Implement the close logic for the scope.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceLifeCycle<A extends Annotation>
-
stop
Description copied from interface:LifeCycleStops the life cycle component.- Specified by:
stopin interfaceLifeCycle<A extends Annotation>- Returns:
- This lifecycle component
-
destroyScope
Destroys the scope.- Parameters:
scopeMap- The scope map
-
getOrCreate
Description copied from interface:CustomScopeResolve an object from the given scope.- Specified by:
getOrCreatein interfaceCustomScope<A extends Annotation>- Type Parameters:
T- The bean generic type- Parameters:
creationContext- The creation context- Returns:
- The bean instance
-
doCreate
@NonNull protected <T> @NonNull CreatedBean<T> doCreate(@NonNull @NonNull BeanCreationContext<T> creationContext) Perform creation.- Type Parameters:
T- The generic type- Parameters:
creationContext- The creation context- Returns:
- Created bean
-
remove
Description copied from interface:CustomScopeRemove a bean definition from the scope.- Specified by:
removein interfaceCustomScope<A extends Annotation>- Type Parameters:
T- The generic type- Parameters:
identifier- TheBeanIdentifier- Returns:
- An
Optionalof the instance that was destroyed if it exists
-
handleDestructionException
Method that can be overridden to customize what happens on a shutdown error.- Parameters:
e- The exception
-
findBeanRegistration
Description copied from interface:CustomScopeGet theBeanDefinitionfor the given bean.- Specified by:
findBeanRegistrationin interfaceCustomScope<A extends Annotation>- Type Parameters:
T- The bean generic type- Parameters:
bean- The bean- Returns:
- The bean definition if it can be resolved
-