📜  春天 – BeanFactory

📅  最后修改于: 2022-05-13 01:54:33.247000             🧑  作者: Mango

春天 – BeanFactory

当我们谈论 spring 时,最重要的事情是依赖注入,这是可能的,因为 spring 实际上是一个容器并且表现为 Bean 的工厂。就像 BeanFactory 接口是最简单的容器一样,它提供了高级配置机制来实例化、配置和管理 bean 的生命周期。 Bean 是由 Spring IoC Container 在运行时配置的Java对象。 BeanFactory 代表一个基本的IoC 容器,它是ApplicationContext的父接口 BeanFactory使用 Bean 及其依赖项元数据在运行时创建和配置它们。 BeanFactory 基于配置文件 (XML) 加载 bean 定义和 bean 之间的依赖关系,或者在需要时使用Java Configuration 直接返回 bean。还有其他类型的配置文件,如 LDAP、RDMS、属性文件等。 BeanFactory 不支持基于注解的配置,而 ApplicationContext 支持。

让我们先来了解一下Bean factory的一些方法,然后再开始实现,下面以表格形式展示如下:

Method

Description



containsBean(String name)Does this bean factory contain a bean definition or externally registered singleton instance with the given name?
getAliases(String name)Return the aliases for the given bean name, if any.
getBean(Class requiredType)Return the bean instance that uniquely matches the given object type, if any.
getBean(Class requiredType, Object… args)Return an instance, which may be shared or independent, of the specified bean.
getBean(String name)Return an instance, which may be shared or independent, of the specified bean.
getBean(String name, Class requiredType)Return an instance, which may be shared or independent, of the specified bean.
getBean(String name, Object… args)Return an instance, which may be shared or independent, of the specified bean.
getBeanProvider(Class requiredType)Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
getBeanProvider(ResolvableType requiredType)Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
getType(String name)Determine the type of the bean with the given name.
getType(String name, boolean allowFactoryBeanInit)Determine the type of the bean with the given name.
isPrototype(String name)Is this bean a prototype? That is, will getBean(java.lang.String) always return independent instances?
isSingleton(String name)Is this bean a shared singleton? That is, will getBean(java.lang.String) always return the same instance?
isTypeMatch(String name, Class typeToMatch)Check whether the bean with the given name matches the specified type.
isTypeMatch(String name, ResolvableType typeToMatch)Check whether the bean with the given name matches the specified type.

程序:

  1. 使用 start.spring.io 创建一个 Spring 项目。
  2. 创建一个 POJO 类。
  3. bean-factory-demo.xml文件中配置 Student bean。
  4. 将其写入应用程序类。

执行:

第 1 步: Bean 定义: 创建一个 Student POJO 类。

// Java Program where we are creating a POJO class

// POJO class
public class Student {

  // Member variables
  private String name;
  private String age;

  // Constructor 1
  public Student() {
  }

  // Constructor 2
  public Student(String name, String age) {
    this.name = name;
    this.age = age;
  }

  // Method inside POJO class
  @Override
  public String toString() {

    // Print student clas attributes
    return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}';
  }
}

第 2 步: XML Bean 配置: bean-factory-demo.xml文件中配置 Student bean。



            
    
                
                
        

第 3 步:主类

// Application class 
@SpringBootApplication

// Main class
public class DemoApplication {

  // Main driver method
  public static void main(String[] args) {

    // Creating object in a spring container (Beans)
    BeanFactory factory = new ClassPathXmlApplicationContext("bean-factory-demo.xml");
    Student student = (Student) factory.getBean("student");

    System.out.println(student);
  }
}

输出:

Student{name='Tina', age='21'}