• Spring之Bean的实例化



    前言

    Spring为Bean提供了多种实例化方法,通常包括四种方式。目的是:更加灵活

    • 第一种:通过构造方法实例化
    • 第二种:通过简单工厂模式实例化
    • 第三种:通过factory-bean实例化
    • 第四种:通过FactoryBean接口实例化

    一、通过构造方法实例化

    准备一个类:
    SpringBean类:
    默认有无参构造方法

    public class SpringBean {
    }
    
    
    • 1
    • 2
    • 3

    准备spring配置文件
    spring-bean.xml
    Spring提供的第一种实例化方式:在spring配置文件中直接配置类全路径,spring会自动调用该类的无参数构造方法来实例化bean

    
    <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="sb" class="com.powernode.spring6.bean.SpringBean">bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    准备测试类:

        @Test
        public void testSpringBean(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
            SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
            System.out.println(sb);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行结果如下:
    对象创建成功

    在这里插入图片描述

    二、通过简单工厂模式实例化

    编写Bean对象:

    /**
     * 明星类(Bean)
     */
    public class Star {
        public Star() {
            System.out.println("star的无参数构造方法执行");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编写工厂类:

    /**
     * 工厂类(明星工厂类)
     * 简单工厂模式中的工厂类角色 星工厂
     */
    public class StarFactory {
        //工厂类中有一个静态方法
        //简单工厂模式又叫做静态工厂模式
        public static Star get(){
            //这个star对象最终实际上创建的时候还是程序员负责new的对象
            return new Star();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    编写spring配置文件:
    Spring提供的第二种实例化方式:通过简单工厂模式,需要在spring配置文件中告诉spring框架,调用哪个类的哪个方法获取bean。
    factory-method属性指的是工厂类中的静态方法,也就是告诉Spring调用这个方法可以获取Bean

    
    <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="starBean" class="com.powernode.spring6.bean.StarFactory" factory-method="get">bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编写测试类:

        @Test
        public void testSpringBean2(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
            Star star = applicationContext.getBean("starBean", Star.class);
            System.out.println(star);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    三、通过factory-bean实例化

    编写Bean对象:

    /**
     * 工厂方法模式当中的具体产品角色
     */
    public class Gun {
        public Gun() {
            System.out.println("GUN的无参数构造方法执行了!");
        }
    
        public GunFactory get() {
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    编写工厂类:
    与简单工厂模式不同的是工厂方法模式中的具体工厂角色中的方法是实例方法,简单工厂模式中的工厂角色中的方法是静态方法

    /**
     * 工厂方法模式当中的 具体工厂角色
     */
    public class GunFactory {
        //工厂方法模式中的具体工厂角色中的方法是实例方法
        public Gun get(){
            //实际上new对象还是程序员自己new的
            return new Gun();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    编写spring配置文件:
    Spring提供的第三种实例化方式:通过工厂方法模式。通过factory-bean属性+factory-method属性来共同完成

    
    <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="gunFactoryBean" class="com.powernode.spring6.bean.GunFactory" >bean>
        
        <bean id="gunBean" factory-method="get" factory-bean="gunFactoryBean">bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    编写测试程序:

        @Test
        public void testSpringBean3(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
            Gun gun = applicationContext.getBean("gunBean", Gun.class);
            System.out.println(gun);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    四、通过FactoryBean接口实例化

    这种方式是通过第三种方式衍变来的。
    在spring中,当编写的类直接实现FactoryBean接口之后,factory-bean不需要指定了,factory-method也不需要指定了。factory-bean会自动指向FactoryBean接口的类,factory-method会自动指向getObject()方法。

    创建一个bean对象:

    public class Person {
        public Person() {
            System.out.println("person的无参数构造方法执行了!!");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建工厂 实现FactoryBean:

    也是一个bean 只不过这个bean比较特殊 叫做工厂bean
    通过工厂bean这个特殊的bean可以获取普通的bean

    public class PersonFactory implements FactoryBean<Person>{
        //这个方法在接口中有默认实现
        //默认返回 true 表示单例
        //如果想多例 直接将这个方法修改为return false即可
        @Override
        public boolean isSingleton() {
            return true;
        }
    
    
        @Override
        public Person getObject() throws Exception {
            //最终这个bean的创建还是程序员自己new的
            return new Person();
        }
    
        @Override
        public Class<?> getObjectType() {
            return null;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    编写spring配置文件:

    通过FactoryBean这个工厂bean主要是想对普通bean进行加工处理

    
    <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="person" class="com.powernode.spring6.bean.PersonFactory">bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编写测试类:

        @Test
        public void testSpringBean4(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");
            Person person = applicationContext.getBean("person", Person.class);
            System.out.println(person);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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


  • 相关阅读:
    sqli-labs/Less-53
    flutter 环境搭建(windows)(先装 jdk 建议1.8起步)
    【kotlin 协程】万字协程 一篇完成kotlin 协程进阶
    SAP router的问题 dev_out 大文件 ,bat 关闭服务,删除文件,重启服务
    2013款别克凯越危险警告灯不亮故障诊断方案设计
    26栈和队列-简单实践
    【代数学习题4.2】从零理解范数与迹 —— 求数域元素的范数与迹
    ESP8266-Arduino编程实例-MQ3酒精传感器驱动
    R语言清洗与处理数据常用代码段
    LVS+keepalived高可用群集
  • 原文地址:https://blog.csdn.net/qq_42338744/article/details/127933933