• 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

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


  • 相关阅读:
    算法题:树中根节点到到目标节点的路径java
    【自然语言处理】【检索】GENER:自回归实体检索
    NCMMSC 2021丨长短视频多语种多模态识别挑战赛
    FinalShell或者XShell工具 突然连不上服务器(绝对好使!)
    语音芯片基础知识 什么是语音芯 他有什么作用 发展趋势是什么
    浏览器复制功能
    通过openssl库计算字符串或文件sha256
    【计算讲谈社】第一讲:支撑 10 万人同时在线互动,是实现元宇宙的基本前提?
    光谱编辑和修复工具:Steinberg SpectraLayers Pro mac
    Open3D 进阶(12)PCA拟合平面
  • 原文地址:https://blog.csdn.net/qq_42338744/article/details/127933933