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

infra.beans.factory.support.SimpleBeanDefinitionRegistry Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 - 2024 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see [https://www.gnu.org/licenses/]
 */

package infra.beans.factory.support;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

import infra.beans.factory.BeanDefinitionStoreException;
import infra.beans.factory.NoSuchBeanDefinitionException;
import infra.beans.factory.config.BeanDefinition;
import infra.core.DefaultAliasRegistry;
import infra.lang.Assert;
import infra.util.StringUtils;

/**
 * Default implementation of the {@link BeanDefinitionRegistry} interface.
 * Provides registry capabilities only, with no factory capabilities built in.
 * Can for example be used for testing bean definition readers.
 *
 * @author TODAY 2021/10/1 14:56
 * @since 4.0
 */
public class SimpleBeanDefinitionRegistry extends DefaultAliasRegistry implements BeanDefinitionRegistry {

  /** Map of bean definition objects, keyed by bean name. */
  private final ConcurrentHashMap beanDefinitionMap = new ConcurrentHashMap<>(64);

  /** Whether to allow re-registration of a different definition with the same name. */
  private boolean allowBeanDefinitionOverriding = true;

  public Map getBeanDefinitions() {
    return beanDefinitionMap;
  }

  @Override
  public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
          throws BeanDefinitionStoreException {

    Assert.hasText(beanName, "'beanName' must not be empty");
    Assert.notNull(beanDefinition, "BeanDefinition is required");
    this.beanDefinitionMap.put(beanName, beanDefinition);
  }

  @Override
  public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
    if (this.beanDefinitionMap.remove(beanName) == null) {
      throw new NoSuchBeanDefinitionException(beanName);
    }
  }

  @Override
  public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
    BeanDefinition bd = this.beanDefinitionMap.get(beanName);
    if (bd == null) {
      throw new NoSuchBeanDefinitionException(beanName);
    }
    return bd;
  }

  @Override
  public BeanDefinition getBeanDefinition(Class beanClass) {
    for (final BeanDefinition definition : getBeanDefinitions().values()) {
      if (definition instanceof AbstractBeanDefinition abd
              && beanClass.isAssignableFrom(abd.getBeanClass())) {
        return definition;
      }
    }
    return null;
  }

  @Override
  public boolean containsBeanDefinition(String beanName) {
    return this.beanDefinitionMap.containsKey(beanName);
  }

  @Override
  public boolean containsBeanDefinition(Class type) {
    return containsBeanDefinition(type, false);
  }

  @Override
  public boolean containsBeanDefinition(final Class type, final boolean equals) {
    final Predicate predicate = getPredicate(type, equals);
    for (final BeanDefinition beanDef : getBeanDefinitions().values()) {
      if (beanDef instanceof AbstractBeanDefinition abd && predicate.test(abd)) {
        return true;
      }
    }
    return false;
  }

  private Predicate getPredicate(final Class type, final boolean equals) {
    return equals
            ? beanDef -> type == beanDef.getBeanClass()
            : beanDef -> type.isAssignableFrom(beanDef.getBeanClass());
  }

  @Override
  public String[] getBeanDefinitionNames() {
    return StringUtils.toStringArray(this.beanDefinitionMap.keySet());
  }

  @Override
  public int getBeanDefinitionCount() {
    return this.beanDefinitionMap.size();
  }

  @Override
  public boolean isBeanNameInUse(String beanName) {
    return isAlias(beanName) || containsBeanDefinition(beanName);
  }

  /**
   * Set whether it should be allowed to override bean definitions by registering
   * a different definition with the same name, automatically replacing the former.
   * If not, an exception will be thrown. This also applies to overriding aliases.
   * 

Default is "true". * * @see #registerBeanDefinition * @since 4.0 */ public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding; } @Override public boolean isAllowBeanDefinitionOverriding() { return allowBeanDefinitionOverriding; } @Override public boolean isBeanDefinitionOverridable(String beanName) { return isAllowBeanDefinitionOverriding(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy