All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.context.BeanDefinitionRegistry Maven / Gradle / Ivy

There is a newer version: 4.7.12
Show newest version
/*
 * 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
 *
 * http://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.exceptions.NoSuchBeanException;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.inject.BeanConfiguration;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.BeanDefinitionReference;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Collection;
import java.util.Optional;

/**
 * 

Core bean definition registry interface containing methods to find {@link BeanDefinition} instances.

* * @author Graeme Rocher * @since 1.0 */ public interface BeanDefinitionRegistry { /** * Return whether the bean of the given type is contained within this context. * * @param beanType The bean type * @param qualifier The qualifier for the bean * @param The concrete type * @return True if it is */ boolean containsBean(@NonNull Class beanType, @Nullable Qualifier qualifier); /** *

Registers a new singleton bean at runtime. This method expects that the bean definition data will have been * compiled ahead of time.

*

*

If bean definition data is found the method will perform dependency injection on the instance followed by * invoking any {@link javax.annotation.PostConstruct} hooks.

*

*

If no bean definition data is found the bean is registered as is.

* * @param type The bean type * @param singleton The singleton bean * @param qualifier The bean qualifier * @param inject Whether the singleton should be injected (defaults to true) * @param The concrete type * @return This bean context */ @NonNull BeanDefinitionRegistry registerSingleton( @NonNull Class type, @NonNull T singleton, @Nullable Qualifier qualifier, boolean inject ); /** * Obtain a bean configuration by name. * * @param configurationName The configuration name * @return An optional with the configuration either present or not */ @NonNull Optional findBeanConfiguration(@NonNull String configurationName); /** * Obtain a {@link BeanDefinition} for the given type. * * @param beanType The type * @param qualifier The qualifier * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ @NonNull Optional> findBeanDefinition(@NonNull Class beanType, @Nullable Qualifier qualifier); /** * Obtain a {@link BeanDefinition} for the given bean. * * @param bean The bean * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ @NonNull Optional> findBeanRegistration(@NonNull T bean); /** * Obtain a {@link BeanDefinition} for the given type. * * @param beanType The type * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ @NonNull Collection> getBeanDefinitions(@NonNull Class beanType); /** * Obtain a {@link BeanDefinition} for the given type. * * @param beanType The type * @param qualifier The qualifier * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ @NonNull Collection> getBeanDefinitions(@NonNull Class beanType, @Nullable Qualifier qualifier); /** * Get all of the {@link BeanDefinition} for the given qualifier. * * @param qualifier The qualifer * @return The bean definitions */ @NonNull Collection> getBeanDefinitions(@NonNull Qualifier qualifier); /** * Get all of the registered {@link BeanDefinition}. * * @return The bean definitions */ @NonNull Collection> getAllBeanDefinitions(); /** * Get all of the enabled {@link BeanDefinitionReference}. * * @return The bean definitions */ @NonNull Collection> getBeanDefinitionReferences(); /** * Find active {@link javax.inject.Singleton} beans for the given qualifier. Note that * this method can return multiple registrations for a given singleton bean instance since each bean may have multiple qualifiers. * * @param qualifier The qualifier * @return The beans */ @NonNull Collection> getActiveBeanRegistrations(@NonNull Qualifier qualifier); /** * Find active {@link javax.inject.Singleton} beans for the given bean type. Note that * this method can return multiple registrations for a given singleton bean instance since each bean may have multiple qualifiers. * * @param beanType The bean type * @param The concrete type * @return The beans */ @NonNull Collection> getActiveBeanRegistrations(@NonNull Class beanType); /** * Find and if necessary initialize {@link javax.inject.Singleton} beans for the given bean type, returning all the active registrations. Note that * this method can return multiple registrations for a given singleton bean instance since each bean may have multiple qualifiers. * * @param beanType The bean type * @param The concrete type * @return The beans */ @NonNull Collection> getBeanRegistrations(@NonNull Class beanType); /** * Obtain the original {@link BeanDefinition} for a {@link io.micronaut.inject.ProxyBeanDefinition}. * * @param beanType The type * @param qualifier The qualifier * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ @NonNull Optional> findProxyTargetBeanDefinition(@NonNull Class beanType, @Nullable Qualifier qualifier); /** * Obtain the original {@link BeanDefinition} for a {@link io.micronaut.inject.ProxyBeanDefinition}. * * @param beanType The type * @param qualifier The qualifier * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ @NonNull Optional> findProxyBeanDefinition(@NonNull Class beanType, @Nullable Qualifier qualifier); /** *

Registers a new singleton bean at runtime. This method expects that the bean definition data will have been * compiled ahead of time.

*

*

If bean definition data is found the method will perform dependency injection on the instance followed by * invoking any {@link javax.annotation.PostConstruct} hooks.

*

*

If no bean definition data is found the bean is registered as is.

* * @param type The bean type * @param singleton The singleton bean * @param qualifier The bean qualifier * @param The concrete type * @return This bean context */ default @NonNull BeanDefinitionRegistry registerSingleton( @NonNull Class type, @NonNull T singleton, @Nullable Qualifier qualifier ) { return registerSingleton(type, singleton, qualifier, true); } /** *

Registers a new singleton bean at runtime. This method expects that the bean definition data will have been * compiled ahead of time.

*

*

If bean definition data is found the method will perform dependency injection on the instance followed by * invoking any {@link javax.annotation.PostConstruct} hooks.

*

*

If no bean definition data is found the bean is registered as is.

* * @param type the bean type * @param singleton The singleton bean * @param The concrete type * @return This bean context */ default BeanDefinitionRegistry registerSingleton( @NonNull Class type, @NonNull T singleton ) { return registerSingleton(type, singleton, null); } /** * Obtain a {@link BeanDefinition} for the given type. * * @param beanType The type * @param qualifier The qualifier * @param The concrete type * @return The {@link BeanDefinition} * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type * @throws NoSuchBeanException If the bean cannot be found */ default @NonNull BeanDefinition getBeanDefinition(@NonNull Class beanType, @Nullable Qualifier qualifier) { return findBeanDefinition(beanType, qualifier).orElseThrow(() -> new NoSuchBeanException(beanType, qualifier)); } /** * Obtain the original {@link BeanDefinition} for a {@link io.micronaut.inject.ProxyBeanDefinition}. * * @param beanType The type * @param qualifier The qualifier * @param The concrete type * @return The {@link BeanDefinition} * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type * @throws NoSuchBeanException If the bean cannot be found */ default @NonNull BeanDefinition getProxyTargetBeanDefinition(@NonNull Class beanType, @Nullable Qualifier qualifier) { return findProxyTargetBeanDefinition(beanType, qualifier).orElseThrow(() -> new NoSuchBeanException(beanType, qualifier)); } /** * Obtain a {@link BeanDefinition} for the given type. * * @param beanType The type * @param The concrete type * @return The {@link BeanDefinition} * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type * @throws NoSuchBeanException If the bean cannot be found */ default @NonNull BeanDefinition getBeanDefinition(@NonNull Class beanType) { return findBeanDefinition(beanType, null).orElseThrow(() -> new NoSuchBeanException(beanType)); } /** * Obtain a {@link BeanDefinition} for the given type. * * @param beanType The type * @param The concrete type * @return An {@link Optional} of the bean definition * @throws io.micronaut.context.exceptions.NonUniqueBeanException When multiple possible bean definitions exist * for the given type */ default @NonNull Optional> findBeanDefinition(@NonNull Class beanType) { return findBeanDefinition(beanType, null); } /** *

Registers a new singleton bean at runtime. This method expects that the bean definition data will have been * compiled ahead of time.

*

*

If bean definition data is found the method will perform dependency injection on the instance followed by * invoking any {@link javax.annotation.PostConstruct} hooks.

*

*

If no bean definition data is found the bean is registered as is.

* * @param singleton The singleton bean * @return This bean context */ default @NonNull BeanDefinitionRegistry registerSingleton(@NonNull Object singleton) { ArgumentUtils.requireNonNull("singleton", singleton); Class type = singleton.getClass(); return registerSingleton(type, singleton); } /** *

Registers a new singleton bean at runtime. This method expects that the bean definition data will have been * compiled ahead of time.

*

*

If bean definition data is found the method will perform dependency injection on the instance followed by * invoking any {@link javax.annotation.PostConstruct} hooks.

*

*

If no bean definition data is found the bean is registered as is.

* * @param singleton The singleton bean * @param inject Whether the singleton should be injected (defaults to true) * @return This bean context */ default @NonNull BeanDefinitionRegistry registerSingleton(@NonNull Object singleton, boolean inject) { ArgumentUtils.requireNonNull("singleton", singleton); Class type = singleton.getClass(); return registerSingleton( type, singleton, null, inject ); } /** * Return whether the bean of the given type is contained within this context. * * @param beanType The bean type * @return True if it is */ @SuppressWarnings("ConstantConditions") default boolean containsBean(@NonNull Class beanType) { return beanType != null && containsBean(beanType, null); } }