Spring之Bean的实例化


前言

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

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

一、通过构造方法实例化

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

public class SpringBean {
}

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

<?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="sb" class="com.powernode.spring6.bean.SpringBean"></bean>
</beans>

准备测试类:

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

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

在这里插入图片描述

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

编写Bean对象:

/**
 * 明星类(Bean)
 */
public class Star {
    public Star() {
        System.out.println("star的无参数构造方法执行");
    }
}

编写工厂类:

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

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

<?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">
    <!--Spring提供的第二种实例化方式:通过简单工厂模式,需要在spring配置文件中告诉spring框架,调用哪个类的哪个方法获取bean-->
    <!--factory-method 属性指的是工厂类中的静态方法,也就是告诉Spring调用这个方法可以获取Bean-->
    <bean id="starBean" class="com.powernode.spring6.bean.StarFactory" factory-method="get"></bean>
</beans>

编写测试类:

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

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

三、通过factory-bean实例化

编写Bean对象:

/**
 * 工厂方法模式当中的具体产品角色
 */
public class Gun {
    public Gun() {
        System.out.println("GUN的无参数构造方法执行了!");
    }

    public GunFactory get() {
        return null;
    }
}

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

/**
 * 工厂方法模式当中的 具体工厂角色
 */
public class GunFactory {
    //工厂方法模式中的具体工厂角色中的方法是实例方法
    public Gun get(){
        //实际上new对象还是程序员自己new的
        return new Gun();
    }
}

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

<?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">
   <!--Spring提供的第三种实例化方式:通过工厂方法模式。通过factory-bean属性+factory-method属性来共同完成-->
    <!--告诉spring框架,调用哪个对象的哪个方法来获取bean-->
    <bean id="gunFactoryBean" class="com.powernode.spring6.bean.GunFactory" ></bean>
    <!--以下配置非常关键,factory-bean属性告诉spring调用哪个对象。factory-method属性告诉spring调用对象的哪个方法-->
    <bean id="gunBean" factory-method="get" factory-bean="gunFactoryBean"></bean>

</beans>

编写测试程序:

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

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

四、通过FactoryBean接口实例化

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

创建一个bean对象:

public class Person {
    public Person() {
        System.out.println("person的无参数构造方法执行了!!");
    }
}

创建工厂 实现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;
    }
}

编写spring配置文件:

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

<?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">
    <!--Spring提供的第四种实例化方式:通过FactoryBean接口来实现-->
    <!--这种方式就是第三种方式的简化,由于编写的类实现了FactoryBean接口,所以这个类是特殊的类,不需要再手动指定factory-bean、factory-method-->
    <bean id="person" class="com.powernode.spring6.bean.PersonFactory"></bean>
</beans>

编写测试类:

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

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值