Package io.micronaut.context.scope
Class AbstractConcurrentCustomScope<A extends java.lang.annotation.Annotation>
- java.lang.Object
-
- io.micronaut.context.scope.AbstractConcurrentCustomScope<A>
-
- Type Parameters:
A- The annotation type
- All Implemented Interfaces:
LifeCycle<AbstractConcurrentCustomScope<A>>,CustomScope<A>,java.io.Closeable,java.lang.AutoCloseable
public abstract class AbstractConcurrentCustomScope<A extends java.lang.annotation.Annotation> extends java.lang.Object implements CustomScope<A>, LifeCycle<AbstractConcurrentCustomScope<A>>, java.lang.AutoCloseable
Abstract implementation of the custom scope interface that simplifies defining new scopes using the Map interface.Note this implementation uses a single
ReentrantReadWriteLockto 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 perBeanIdentifier.- Since:
- 3.0.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractConcurrentCustomScope(java.lang.Class<A> annotationType)A custom scope annotation.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.Class<A>annotationType()abstract voidclose()Implement the close logic for the scope.protected voiddestroyScope(java.util.Map<BeanIdentifier,CreatedBean<?>> scopeMap)Destroys the scope.protected <T> CreatedBean<T>doCreate(BeanCreationContext<T> creationContext)Perform creation.<T> java.util.Optional<BeanRegistration<T>>findBeanRegistration(T bean)Get theBeanDefinitionfor the given bean.<T> TgetOrCreate(BeanCreationContext<T> creationContext)Resolve an object from the given scope.protected abstract java.util.Map<BeanIdentifier,CreatedBean<?>>getScopeMap(boolean forCreation)protected voidhandleDestructionException(BeanDestructionException e)Method that can be overridden to customize what happens on a shutdown error.<T> java.util.Optional<T>remove(BeanIdentifier identifier)Remove a bean definition from the scope.AbstractConcurrentCustomScope<A>stop()Stops the life cycle component.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.micronaut.context.scope.CustomScope
findBeanRegistration
-
-
-
-
Constructor Detail
-
AbstractConcurrentCustomScope
protected AbstractConcurrentCustomScope(java.lang.Class<A> annotationType)
A custom scope annotation.- Parameters:
annotationType- The annotation type
-
-
Method Detail
-
getScopeMap
@NonNull protected abstract java.util.Map<BeanIdentifier,CreatedBean<?>> getScopeMap(boolean forCreation)
- Parameters:
forCreation- Whether it is for creation- Returns:
- Obtains the scope map, never null
- Throws:
java.lang.IllegalStateException- if the scope map cannot be obtained in the current context
-
annotationType
public final java.lang.Class<A> annotationType()
- Specified by:
annotationTypein interfaceCustomScope<A extends java.lang.annotation.Annotation>- Returns:
- The scope annotation type
-
close
public abstract void close()
Implement the close logic for the scope.
-
stop
@NonNull public final AbstractConcurrentCustomScope<A> stop()
Description copied from interface:LifeCycleStops the life cycle component.
-
destroyScope
protected void destroyScope(@Nullable java.util.Map<BeanIdentifier,CreatedBean<?>> scopeMap)Destroys the scope.- Parameters:
scopeMap- Th scope map
-
getOrCreate
public final <T> T getOrCreate(BeanCreationContext<T> creationContext)
Description copied from interface:CustomScopeResolve an object from the given scope.- Specified by:
getOrCreatein interfaceCustomScope<A extends java.lang.annotation.Annotation>- Type Parameters:
T- The bean generic type- Parameters:
creationContext- The creation context- Returns:
- The bean instance
-
doCreate
@NonNull protected <T> CreatedBean<T> doCreate(@NonNull BeanCreationContext<T> creationContext)
Perform creation.- Type Parameters:
T- The generic type- Parameters:
creationContext- The creation context- Returns:
- Created bean
-
remove
public final <T> java.util.Optional<T> remove(BeanIdentifier identifier)
Description copied from interface:CustomScopeRemove a bean definition from the scope.- Specified by:
removein interfaceCustomScope<A extends java.lang.annotation.Annotation>- Type Parameters:
T- The generic type- Parameters:
identifier- TheBeanIdentifier- Returns:
- An
Optionalof the instance that was destroyed if it exists
-
handleDestructionException
protected void handleDestructionException(BeanDestructionException e)
Method that can be overridden to customize what happens on a shutdown error.- Parameters:
e- The exception
-
findBeanRegistration
public final <T> java.util.Optional<BeanRegistration<T>> findBeanRegistration(T bean)
Description copied from interface:CustomScopeGet theBeanDefinitionfor the given bean.- Specified by:
findBeanRegistrationin interfaceCustomScope<A extends java.lang.annotation.Annotation>- Type Parameters:
T- The bean generic type- Parameters:
bean- The bean- Returns:
- The bean definition if it can be resolved
-
-