Class DefaultBringBeanFactory

java.lang.Object
com.bobocode.bring.core.context.impl.DefaultBringBeanFactory
All Implemented Interfaces:
BringBeanFactory
Direct Known Subclasses:
AnnotationBringBeanRegistry

public class DefaultBringBeanFactory extends Object implements BringBeanFactory
Default implementation of the BringBeanFactory interface providing basic bean management functionalities. Manages bean definitions, singleton, and prototype beans.
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

  • Constructor Details

    • DefaultBringBeanFactory

      public DefaultBringBeanFactory()
  • Method Details

    • getBean

      public <T> T getBean(Class<T> type)
      Retrieves a bean of the given type from the bean factory. If multiple beans of the same type exist, returns the primary one.
      Specified by:
      getBean in interface BringBeanFactory
      Type Parameters:
      T - The type of the bean.
      Parameters:
      type - The class type of the bean to retrieve.
      Returns:
      The bean instance.
      Throws:
      NoSuchBeanException - If no bean of the given type is found.
      NoUniqueBeanException - If more than one primary bean of the given type is found.
    • getBean

      public <T> T getBean(Class<T> type, String name)
      Retrieves a bean of the given type and name from the bean factory.
      Specified by:
      getBean in interface BringBeanFactory
      Type Parameters:
      T - The type of the bean.
      Parameters:
      type - The class type of the bean to retrieve.
      name - The name of the bean.
      Returns:
      The bean instance.
      Throws:
      NoSuchBeanException - If no bean of the given type and name is found.
    • getBeans

      public <T> Map<String,T> getBeans(Class<T> type)
      Retrieves all beans of the specified type from the factory.
      Specified by:
      getBeans in interface BringBeanFactory
      Type Parameters:
      T - The type of the beans.
      Parameters:
      type - The type of the beans to retrieve.
      Returns:
      A map of bean names to their respective instances.
    • getBeanByName

      public Object getBeanByName(String beanName)
      Retrieves a bean instance by its name.
      Parameters:
      beanName - The name of the bean.
      Returns:
      The retrieved bean instance.
    • getAllBeans

      public <T> Map<String,T> getAllBeans()
      Retrieves all singleton beans registered in the factory.
      Specified by:
      getAllBeans in interface BringBeanFactory
      Type Parameters:
      T - The type of the beans.
      Returns:
      A map of singleton bean names to their respective instances.
    • close

      public void close()
      Description copied from interface: BringBeanFactory
      Closes the container.This method is typically called when the container is being shut down to allow beans to perform necessary cleanup operations or release resources before being destroyed.
      Specified by:
      close in interface BringBeanFactory
    • addSingletonBean

      void addSingletonBean(String beanName, Object bean)
      Adds a singleton bean to the factory with the given name and instance.
      Parameters:
      beanName - The name of the singleton bean.
      bean - The instance of the singleton bean to be added.
    • addPrototypeBean

      void addPrototypeBean(String beanName, Supplier<Object> supplier)
      Adds a prototype bean to the factory with the given name and supplier.
      Parameters:
      beanName - The name of the prototype bean.
      supplier - The supplier supplying instances of the prototype bean.
    • addBeanDefinition

      public void addBeanDefinition(String beanName, BeanDefinition beanDefinition)
      Adds a bean definition to the factory with the given name and definition. Updates the type-to-bean-names mapping accordingly.
      Parameters:
      beanName - The name of the bean.
      beanDefinition - The definition of the bean to be added.
    • getBeanDefinitionByName

      public BeanDefinition getBeanDefinitionByName(String beanName)
      Retrieves the bean definition associated with the given bean name.
      Parameters:
      beanName - The name of the bean for which the definition is requested.
      Returns:
      The BeanDefinition object associated with the provided bean name.
    • getAllBeanDefinitionNames

      public List<String> getAllBeanDefinitionNames()
      Retrieves a list containing names of all registered bean definitions in the factory.
      Returns:
      A list containing the names of all registered bean definitions.
    • isBeanCreated

      public boolean isBeanCreated(String beanName)
      Checks if a bean with the specified name has been created.
      Parameters:
      beanName - The name of the bean to check.
      Returns:
      true if the bean has been created, false otherwise.
    • getPrimary

      private <T> T getPrimary(Map<String,T> beans, Class<T> type)