Class ConstructorBeanInjection

java.lang.Object
com.bobocode.bring.core.context.impl.ConstructorBeanInjection

public class ConstructorBeanInjection extends Object
Responsible for facilitating constructor-based dependency injection, the ConstructorBeanInjection class assists in creating beans by instantiating classes through constructors annotated with @Autowired or utilizing a single constructor without explicit @Autowired.
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

  • Constructor Details

    • ConstructorBeanInjection

      public ConstructorBeanInjection()
  • Method Details

    • create

      public Object create(Class<?> clazz, String beanName, BeanDefinition beanDefinition)
      Creates a bean using constructor-based dependency injection for the provided class and bean definition.
      Parameters:
      clazz - The class for which the bean is to be created.
      beanName - The name of the bean.
      beanDefinition - The definition of the bean.
      Returns:
      The created bean object.
      Throws:
      NoConstructorWithAutowiredAnnotationBeanException - If no constructor is annotated with @Autowired.
      NoSuchBeanException - If a required dependency bean is not found.
    • findAutowiredConstructor

      private Constructor<?> findAutowiredConstructor(Class<?> clazz)
      Finds a constructor annotated with @Autowired within the provided class. If multiple constructors exist, the one with the @Autowired annotation is selected or one without.
      Parameters:
      clazz - The class to search for an @Autowired annotated constructor.
      Returns:
      The @Autowired annotated constructor.
      Throws:
      NoConstructorWithAutowiredAnnotationBeanException - If no constructor is annotated with @Autowired.
    • createBeanUsingConstructor

      private Object createBeanUsingConstructor(Constructor<?> constructor, String beanName, BeanDefinition beanDefinition)
      Creates a bean using the specified constructor and resolves its dependencies. Instantiates the bean within the context based on resolved dependencies and the constructor.
      Parameters:
      constructor - The constructor to use for bean instantiation.
      beanName - The name of the bean to be created.
      beanDefinition - The definition of the bean being created.
      Returns:
      The created bean object using constructor.
      Throws:
      NoSuchBeanException - If a required dependency bean is not found.
    • findBeanNameForArgumentInConstructor

      private String findBeanNameForArgumentInConstructor(Parameter parameter, String constructorParamName)
      Finds the appropriate bean name for an argument in a constructor based on its parameter type. Handles scenarios with multiple candidate beans, qualifiers, and primary beans.
      Parameters:
      parameter - The parameter for which the bean name is to be resolved.
      constructorParamName - The name of parameter for which the bean name is to be resolved.
      Returns:
      The resolved bean name for the parameter.
      Throws:
      NoSuchBeanException - If a required bean is not found.
      NoUniqueBeanException - If multiple primary beans or qualifiers match the parameter type.
    • findPrimaryBeanNameOrByQualifierOrByParameter

      private String findPrimaryBeanNameOrByQualifierOrByParameter(List<String> beanNames, String paramName, Parameter parameter)
      Resolves the primary bean name or selects a bean by qualifier or parameter name.
      Parameters:
      beanNames - The candidate bean names for the parameter type.
      paramName - The parameter name in the constructor.
      parameter - The parameter for which the bean name is to be resolved.
      Returns:
      The resolved bean name based on primary, qualifier, or parameter name.
      Throws:
      NoSuchBeanException - If a required bean is not found.
      NoUniqueBeanException - If multiple primary beans or qualifiers match the parameter type.