Spring学习之Bean的实例化的三种方式

        我们已经了解了Spring的基本应用,接下来学习Bean的实例化的三种方式。
        什么是Spring中的Bean?
        假设我们把Spring看做一个工厂,则Spring容器中的Bean就是该工厂的产品。如果我们要想使用工厂生产和管理Bean,就需要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起。Bean的本质是Java中的类,而Spring中的Bean就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean 。

        了解了Spring中的Bean,我们来实现Bean的实例化,而实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化和实例工厂方式实例化。接下来我对每种方式进行详细的解释以及实现。
        1.构造器实例化
        构造器实例化是指Spring容器通过Bean对应类中默认的无参构造方法来实例化Bean。
        最常用的初始化bean方式, 必须提供默认构造方法.
           (1).打开Eclipse,在SpringTest项目的src目录下,创建一个com.example.constructor包,包中创建ConstructorBean类。

package com.example.constructor;

public class ConstructorBean {

}

           (2).在com.example.constructor包中,创建Spring的配置文件beans.xml,在xml文件中定义id为constructorbean,class属性指定对应的实现类ConstructorBean。


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="constructor" class="com.example.constructor.ConstructorBean"></bean>
   
</beans>

           (3).在com.example.constructor包中,创建测试类TestConstructor。

package com.example.constructor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestConstructor {
 public static void main(String[] args) {
  String xmlPath = "com/example/constructor/beans.xml";
  ApplicationContext ac = 
    new ClassPathXmlApplicationContext(xmlPath);
  ConstructorBean constructorBean = (ConstructorBean) ac.getBean("constructor");
  System.out.println(constructorBean);
 }
}

           运行结果:

在这里插入图片描述

        2.静态工厂方式实例化
        该方式需要创建一个静态工厂的方法来创建Bean的实例,其Bean配置中的class属性所指定的是静态工厂类,同时还需要使用factory-method属性指定创建的静态工厂方法。
           (1).在SpringTest项目的src目录下,创建一个com.example.static_factory包,包中创建Static_factoryBean类。

package com.example.static_factory;

public class Static_factoryBean {

}

           (2).在com.example.static_factory包中,创建一个MyFactory类,在类中创建静态方法createBean()。

package com.example.static_factory;

public class MyFactory {
 public static Static_factoryBean createBean(){
  return new Static_factoryBean();
 }
}

           (3).在com.example.static_factory包中,创建Spring的配置文件beans.xml。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="factorybean" class="com.example.static_factory.MyFactory"
    factory-method="createBean"></bean>
</beans>

           (4).在com.example.static_factory包中,创建测试类TestStatic_factory。

package com.example.static_factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStatic_factory {
 public static void main(String[] args) {
  String xmlPath = "com/example/static_factory/beans.xml";
  ApplicationContext ac = 
    new ClassPathXmlApplicationContext(xmlPath);
  Static_factoryBean static_factoryBean = (Static_factoryBean) ac.getBean("factorybean");
  System.out.println(static_factoryBean);
 }
}

           运行结果:
在这里插入图片描述

        3.实例工厂方式实例化
        该方式不需要使用静态方法创建Bean实例,而是采用直接创建Bean实例的方式。在配置文件中,通过factory-bean属性指向配置的实例工厂,使用factory-method属性确定使用哪个方法。
           (1).在SpringTest项目的src目录下,创建一个com.example.factory包,包中创建FactoryBean类。

package com.example.factory;

public class FactoryBean {

}

           (2).在com.example.factory包中,创建一个MyFactory类,使用createBean()方法创建对象。

package com.example.factory;

public class MyFactory {
 public MyFactory() {
  System.out.println("实例工厂实例化");
 }
 public MyFactory createBean(){
  return new MyFactory();
 }
}

           (3).在com.example.factory包中,创建Spring的配置文件beans.xml。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean id="myfactorybean" class="com.example.factory.MyFactory"></bean>
     <bean id="beans" factory-bean="myfactorybean"
     factory-method="createBean"></bean>
</beans>

           (4).在com.example.static_factory包中,创建测试类TestFactory。

package com.example.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFactory {
 public static void main(String[] args) {
  String xmlPath = "com/example/factory/beans.xml";
  ApplicationContext ac = 
    new ClassPathXmlApplicationContext(xmlPath);
  FactoryBean factoryBean = (FactoryBean) ac.getBean("beans");
  System.out.println(factoryBean);
 }
}

           运行结果:
在这里插入图片描述

这就是Bean的实例化的三种方式,若有错漏,欢迎指正,希望大家一起学习进步!!!!
如果转载以及CV操作,请务必注明出处,谢谢!

猜你喜欢

转载自blog.csdn.net/weixin_43468667/article/details/88410866