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
ModifierConstructorDescriptionprotected
AbstractConcurrentCustomScope
(Class<A> annotationType) A custom scope annotation. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
close()
Implement the close logic for the scope.protected void
destroyScope
(@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 theBeanDefinition
for the given bean.final <T> T
getOrCreate
(BeanCreationContext<T> creationContext) Resolve an object from the given scope.protected abstract @NonNull Map<BeanIdentifier,
CreatedBean<?>> getScopeMap
(boolean forCreation) protected void
Method 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, wait
Methods 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:
annotationType
in interfaceCustomScope<A extends Annotation>
- Returns:
- The scope annotation type
-
close
public abstract void close()Implement the close logic for the scope.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceLifeCycle<A extends Annotation>
-
stop
Description copied from interface:LifeCycle
Stops the life cycle component.- Specified by:
stop
in interfaceLifeCycle<A extends Annotation>
- Returns:
- This lifecycle component
-
destroyScope
Destroys the scope.- Parameters:
scopeMap
- Th scope map
-
getOrCreate
Description copied from interface:CustomScope
Resolve an object from the given scope.- Specified by:
getOrCreate
in 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:CustomScope
Remove a bean definition from the scope.- Specified by:
remove
in interfaceCustomScope<A extends Annotation>
- Type Parameters:
T
- The generic type- Parameters:
identifier
- TheBeanIdentifier
- Returns:
- An
Optional
of 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:CustomScope
Get theBeanDefinition
for the given bean.- Specified by:
findBeanRegistration
in interfaceCustomScope<A extends Annotation>
- Type Parameters:
T
- The bean generic type- Parameters:
bean
- The bean- Returns:
- The bean definition if it can be resolved
-