当前位置: 首页 > article >正文

Bean的实例化方式

Bean实例化方式

Bean的实例化方式有四种:
  1. 类构造器方法实例化
  2. 简单工厂模式(静态工厂方法)实例化
  3. 工厂模式(实例工厂方法)实例化
  4. FactoryBean接口实例化

代码示例

类构造器实例化

<?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="userBean" class="com.powernode.entity.User"/>
</beans>
import com.powernode.entity.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 10:53
 */
public class constructionTest {

    // Java原始创建bean
    @Test
    public void Test(){
        User user = new User();
    }

    // 
    @Test
    public void SpringTest(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("construction-beans.xml");
        User userBean = beans.getBean("userBean", User.class);
    }
}

简单工厂模式实例化

第一步:定义一个实体类
package com.powernode.entity;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 11:12
 */
public class Vip {
}

第二步:定义一个工厂类

package com.powernode.factory;

import com.powernode.entity.Vip;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 11:12
 */
public class VipFactory {
    public static Vip getVip(){
        System.out.println("简单工厂模式实例化");
        return new Vip();
    }
}

第三步:实现Spring配置类,class指向BeanFactory类,factory-method指向获取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的实例化
  class 表示获取bean实例化的工厂类
  factory-method 表示通过哪个静态方法获取bean
  -->
  <bean id="VipBean" class="com.powernode.factory.VipFactory" factory-method="getVip"/>
</beans>
import com.powernode.entity.Vip;
import com.powernode.factory.VipFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 10:54
 */
public class staticFactoryTest {

    // JAVA原始 - 通过静态工厂方法实例化
    @Test
    public void Test(){
        Vip vip = VipFactory.getVip();
    }

    // Spring - 通过静态工厂方法实例化
    @Test
    public void SpringTest(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("staticFactory-beans.xml");
        Vip vipBean = beans.getBean("VipBean", Vip.class);
        //简单工厂模式实例
    }
}

工厂模式实例化

第一步:定义一个实体类Order
package com.powernode.entity;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 11:26
 */
public class Order {
}

第二步:定义一个BeanFactory工厂类

package com.powernode.factory;

import com.powernode.entity.Order;
import com.powernode.entity.Vip;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 11:26
 */
public class OrderFactory {
    public Order getOrder(){
        System.out.println("工厂模式实例化");
        return new Order();
    }
}

第三步:实现Spring配置文件。先将工厂类交给Spring IoC 托管,然后实体类通过指定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">
  <!-- 实例化工厂类 -->
  <bean id="factoryBean" class="com.powernode.factory.OrderFactory"/>

  <!-- 实例化实体类 -->
  <bean id="orderBean" class="com.powernode.entity.Order" factory-bean="factoryBean" factory-method="getOrder"/>
</beans>

第四步:测试程序

import com.powernode.entity.Order;
import com.powernode.entity.Vip;
import com.powernode.factory.OrderFactory;
import com.powernode.factory.VipFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 10:54
 */
public class beanFactoryTest {

    // JAVA原始 - 通过实例工厂方法实例化
    @Test
    public void Test(){
        OrderFactory orderFactory = new OrderFactory();
        Order order = orderFactory.getOrder();
    }

    // Spring - 通过实例工厂方法实例化
    @Test
    public void SpringTest(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("beanFactory-beans.xml");
        Order orderBean = beans.getBean("orderBean", Order.class);
        //工厂模式实例
    }
}

FactoryBean实例化

简单工厂模式和工厂模式实例化都需要我们自己定义factory-bean 和 factory-method。

在Spring中,当你编写的类实现了FactoryBean接口之后,factory-bean和factory-mehtod就不要我们自己指定了。

factory-bean会自动指向FactoryBean接口的类,factory-method会自动指向getObject()方法。

注意:FactoryBean在Spring中是一个接口,被称为"工厂Bean",是一个特殊的Bean。只是用来协助Spring框架来创建其他Bean对象的,本身不能够创建Bean对象。

第一步:定义一个Bean

package com.powernode.entity;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 11:36
 */
public class Person {
}

第二步:编写一个类实现FactoryBean接口

package com.powernode.factory;

import com.powernode.entity.Person;
import org.springframework.beans.factory.FactoryBean;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 11:36
 */
public class PersonFactory implements FactoryBean<Person> {
    @Override
    public Person getObject() throws Exception {
        return new Person();
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        /**
         *  true表示单例
         *  false表示原型
         *  默认为true
         */
        return FactoryBean.super.isSingleton();
    }
}

第三步:在Spring配置文件中配置FactoryBean。实现了FactoryBean接口后,Spring会从class的全限定类名指定factory-bean,然后factory-method默认指定getObject()

<?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="personBean" class="com.powernode.factory.PersonFactory"/>
</beans>

第四步:测试程序

import com.powernode.entity.Person;
import com.powernode.entity.Vip;
import com.powernode.factory.PersonFactory;
import com.powernode.factory.VipFactory;
import org.junit.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: 
 * @Description: TODO
 * @DateTime: 2024/9/25 10:54
 */
public class factoryBeanTest {

    // JAVA原始 - 通过FactoryBean辅助实例化
    @Test
    public void Test() throws Exception {
        PersonFactory personFactory = new PersonFactory();
        Person object = personFactory.getObject();
    }

    // Spring - 通过FactoryBean辅助实例化
    @Test
    public void SpringTest(){
        ApplicationContext beans = new ClassPathXmlApplicationContext("factoryBean-beans.xml");
        Person personBean = beans.getBean("personBean", Person.class);
    }
}

BeanFactory 和 FactoryBean的区别

**BeanFactory**是Spring IoC容器的顶级对象,被称为"Bean工厂",负责创建Bean对象;

FactoryBean是一个能够辅助Spring实例化其他Bean对象的一个特殊Bean。

简单来说,BeanFactory是一个工厂,而FactoryBean是一个Bean


http://www.kler.cn/news/339882.html

相关文章:

  • 图解 Transformer
  • 基于Kafka2.1解读Producer原理
  • 【LeetCode刷题记录】45.跳跃游戏 II
  • 45岁被裁员的程序员,何去何从?
  • 等保测评:企业如何进行安全的软件更新与补丁管理
  • 如何设计三极管放大电路?
  • 上海AI Lab视频生成大模型书生.筑梦环境搭建推理测试
  • 正则表达式【JavaScript】
  • Spring Boot快速入门:HelloWorld示例
  • UI自动化测试示例:python+pytest+selenium+allure
  • SDUT数据结构与算法第一次机测
  • SQLITE 构建多表查询
  • UE4 材质学习笔记03(翻书(Flipbook)动画/环境混合)
  • JUC高并发编程6:Callable接口
  • 海报设计模板免费的好用吗?活动海报排版技巧轻松get
  • class 004 选择 冒泡 插入排序
  • CNAI趋势下,打造一体化AI赋能平台
  • 浅学React和JSX
  • Redis 实现 查找附近的人 功能
  • 如何优化spotbugsXml.xml文件来方便debug的落地方案来了