All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.micronaut.context.BeanContext Maven / Gradle / Ivy
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.context;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.core.annotation.AnnotationMetadataResolver;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.attr.MutableAttributeHolder;
import io.micronaut.core.convert.ConversionServiceProvider;
import io.micronaut.core.type.Argument;
import io.micronaut.inject.BeanIdentifier;
import io.micronaut.inject.validation.BeanDefinitionValidator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* The core BeanContext abstraction which allows for dependency injection of classes annotated with
* {@link jakarta.inject.Inject}.
*
* Apart of the standard {@code jakarta.inject} annotations for dependency injection, additional annotations within
* the {@code io.micronaut.context.annotation} package allow control over configuration of the bean context.
*
* @author Graeme Rocher
* @since 1.0
*/
public interface BeanContext extends
LifeCycle,
ExecutionHandleLocator,
BeanLocator,
BeanDefinitionRegistry,
ApplicationEventPublisher,
AnnotationMetadataResolver,
MutableAttributeHolder,
ConversionServiceProvider {
/**
* Obtains the configuration for this context.
* @return The {@link io.micronaut.context.BeanContextConfiguration}
* @since 3.0.0
*/
@NonNull BeanContextConfiguration getContextConfiguration();
/**
* Obtain an {@link io.micronaut.context.event.ApplicationEventPublisher} for the given even type.
* @param eventType The event type
* @param The event generic type
* @return The event publisher, never {@code null}
*/
@SuppressWarnings("unchecked")
default @NonNull ApplicationEventPublisher getEventPublisher(@NonNull Class eventType) {
Objects.requireNonNull(eventType, "Event type cannot be null");
return getBean(Argument.of(ApplicationEventPublisher.class, eventType));
}
/**
* Inject an existing instance.
*
* @param instance The instance to inject
* @param The bean generic type
* @return The instance to inject
*/
@NonNull
T inject(@NonNull T instance);
/**
* Creates a new instance of the given bean performing dependency injection and returning a new instance.
*
* Note that the instance returned is not saved as a singleton in the context.
*
* @param beanType The bean type
* @param The bean generic type
* @return The instance
*/
default @NonNull
T createBean(@NonNull Class beanType) {
return createBean(beanType, (Qualifier) null);
}
/**
* Creates a new instance of the given bean performing dependency injection and returning a new instance.
*
* Note that the instance returned is not saved as a singleton in the context.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The bean generic type
* @return The instance
*/
@NonNull
T createBean(@NonNull Class beanType, @Nullable Qualifier qualifier);
/**
* Creates a new instance of the given bean performing dependency injection and returning a new instance.
*
* If the bean defines any {@link io.micronaut.context.annotation.Parameter} values then the values passed
* in the {@code argumentValues} parameter will be used
*
* Note that the instance returned is not saved as a singleton in the context.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param argumentValues The argument values
* @param The bean generic type
* @return The instance
*/
@NonNull
T createBean(@NonNull Class beanType, @Nullable Qualifier qualifier, @Nullable Map argumentValues);
/**
* Creates a new instance of the given bean performing dependency injection and returning a new instance.
*
* If the bean defines any {@link io.micronaut.context.annotation.Parameter} values then the values passed in
* the {@code argumentValues} parameter will be used
*
* Note that the instance returned is not saved as a singleton in the context.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param args The argument values
* @param The bean generic type
* @return The instance
*/
@NonNull
T createBean(@NonNull Class beanType, @Nullable Qualifier qualifier, @Nullable Object... args);
/**
* Creates a new instance of the given bean performing dependency injection and returning a new instance.
*
* If the bean defines any {@link io.micronaut.context.annotation.Parameter} values then the values passed in
* the {@code argumentValues} parameter will be used
*
* Note that the instance returned is not saved as a singleton in the context.
*
* @param beanType The bean type
* @param args The argument values
* @param The bean generic type
* @return The instance
*/
@NonNull
default T createBean(@NonNull Class beanType, @Nullable Object... args) {
return createBean(beanType, null, args);
}
/**
* Creates a new instance of the given bean performing dependency injection and returning a new instance.
*
* If the bean defines any {@link io.micronaut.context.annotation.Parameter} values then the values passed in
* the {@code argumentValues} parameter will be used
*
* Note that the instance returned is not saved as a singleton in the context.
*
* @param beanType The bean type
* @param argumentValues The argument values
* @param The bean generic type
* @return The instance
*/
@NonNull
default T createBean(@NonNull Class beanType, @Nullable Map argumentValues) {
return createBean(beanType, null, argumentValues);
}
/**
* Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be
* destroyed and removed from the context, otherwise null will be returned.
*
* @param beanType The bean type
* @param The concrete class
* @return The destroy instance or null if no such bean exists
*/
@Nullable
T destroyBean(@NonNull Class beanType);
/**
* Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be
* destroyed and removed from the context, otherwise null will be returned.
*
* @param beanType The bean type
* @param The concrete class
* @return The destroy instance or null if no such bean exists
* @since 3.0.0
*/
@Nullable
default T destroyBean(@NonNull Argument beanType) {
return destroyBean(beanType, null);
}
/**
* Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be
* destroyed and removed from the context, otherwise null will be returned.
*
* @param beanType The bean type
* @param qualifier The qualifier
* @param The concrete class
* @return The destroy instance or null if no such bean exists
* @since 3.0.0
*/
@Nullable
T destroyBean(@NonNull Argument beanType, @Nullable Qualifier qualifier);
/**
* Destroys the given bean.
*
* @param bean The bean
* @param The concrete class
* @return The destroy instance
* @since 3.0.0
*/
@NonNull
T destroyBean(@NonNull T bean);
/**
* Destroys the given bean.
*
* @param beanRegistration The bean registration
* @param The bean type
* @since 3.5.0
*/
@NonNull
void destroyBean(@NonNull BeanRegistration beanRegistration);
/**
* Refresh the state of the given registered bean applying dependency injection and configuration wiring again.
*
* Note that if the bean was produced by a {@link io.micronaut.context.annotation.Factory} then this method will
* refresh the factory too
*
* @param identifier The {@link BeanIdentifier}
* @param The concrete class
* @return An {@link Optional} of the instance if it exists for the given registration
*/
@NonNull
Optional refreshBean(@Nullable BeanIdentifier identifier);
/**
* Refresh the state of the given registered bean applying dependency injection and configuration wiring again.
*
* Note that if the bean was produced by a {@link io.micronaut.context.annotation.Factory} then this method will
* refresh the factory too
*
* This methods skips an additional resolution of the {@link BeanRegistration}.
*
* @param beanRegistration The {@link BeanRegistration}
* @param The concrete class
* @since 3.5.0
*/
@NonNull
void refreshBean(@NonNull BeanRegistration beanRegistration);
/**
* @return The class loader used by this context
*/
@NonNull
ClassLoader getClassLoader();
/**
* @return Get the configured bean validator, if any.
*/
@NonNull
BeanDefinitionValidator getBeanValidator();
@Override
@NonNull
BeanContext registerSingleton(@NonNull Class type, @NonNull T singleton, @Nullable Qualifier qualifier, boolean inject);
@Override
default BeanContext registerSingleton(@NonNull Object singleton) {
Objects.requireNonNull(singleton, "Argument [singleton] must not be null");
Class type = singleton.getClass();
return registerSingleton(type, singleton);
}
@Override
default BeanContext registerSingleton(Class type, T singleton, Qualifier qualifier) {
return registerSingleton(type, singleton, qualifier, true);
}
@Override
default BeanContext registerSingleton(Class type, T singleton) {
return registerSingleton(type, singleton, null, true);
}
@NonNull
@Override
default BeanContext registerSingleton(@NonNull Object singleton, boolean inject) {
return (BeanContext) BeanDefinitionRegistry.super.registerSingleton(singleton, inject);
}
/**
* Run the {@link BeanContext}. This method will instantiate a new {@link BeanContext} and call {@link #start()}.
*
* @return The running {@link BeanContext}
*/
static @NonNull
BeanContext run() {
return build().start();
}
/**
* Build a {@link BeanContext}.
*
* @return The built, but not yet running {@link BeanContext}
*/
static @NonNull
BeanContext build() {
return new DefaultBeanContext();
}
/**
* Run the {@link BeanContext}. This method will instantiate a new {@link BeanContext} and call {@link #start()}.
*
* @param classLoader The classloader to use
* @return The running {@link BeanContext}
*/
static @NonNull
BeanContext run(ClassLoader classLoader) {
return build(classLoader).start();
}
/**
* Build a {@link BeanContext}.
*
* @param classLoader The classloader to use
* @return The built, but not yet running {@link BeanContext}
*/
static @NonNull
BeanContext build(ClassLoader classLoader) {
return new DefaultBeanContext(classLoader);
}
}