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

Spring学习笔记之Bean的实例化方式

文章目录

  • 通过构造方法实例化
  • 通过简单工厂模式实例化
  • 通过factory-bean实例化
  • BeanFactory和FactoryBean的区别
    • BeanFactory
    • FactoryBean
  • 注入自定义Date

Spring为Bean提供了多种实例化方式,通常包括4种方式。(也就是说在Spring中为Bean对象的创建准备了很多种方案,目的是:更加灵活)

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

通过构造方法实例化

默认情况下,会调用Bean的无参数构造

//User
package com.powernode.spring6.bean;public class User {public User() {System.out.println("User类的无参数构造方法执行。");}
}
<!-- spring.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="userBean" class="com.powernode.spring6.bean.User"/></beans>
//test
@Test
public void testConstructor(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");User user = applicationContext.getBean("userBean", User.class);System.out.println(user);
}

通过简单工厂模式实例化

  1. 定义一个Bean
package com.powernode.spring6.bean;public class Vip {
}
  1. 编写简单工厂模式当中的工厂类
package com.powernode.spring6.bean;public class VipFactory {public static Vip get(){return new Vip();}
}
  1. 在Spring配置文件中指定创建该Bean的方法(使用factory-method属性指定)
<bean id="vipBean" class="com.powernode.spring6.bean.VipFactory" factory-method="get"/>
  1. 编写测试程序
@Test
public void testSimpleFactory(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");Vip vip = applicationContext.getBean("vipBean", Vip.class);System.out.println(vip);
}

通过factory-bean实例化

本质上是通过工厂方法模式进行实例化

  1. 定义一个Bean
package com.powernode.spring6.bean;public class Order {
}
  1. 定义具体工厂类,工厂类中定义实例方法
package com.powernode.spring6.bean;public class OrderFactory {public Order get(){return new Order();}
}
  1. 在Spring配置文件中指定factory-bean以及factory-method
<bean id="orderFactory" class="com.powernode.spring6.bean.OrderFactory"/>
<bean id="orderBean" factory-bean="orderFactory" factory-method="get"/>
  1. test
@Test
public void testSelfFactoryBean(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");Order orderBean = applicationContext.getBean("orderBean", Order.class);System.out.println(orderBean);
}

FactoryBean在Spring中是一个接口,被称为“工厂Bean”。“工厂Bean”是一种特殊的Bean。所有的“工厂Bean”都是用来协助Spring框架来创建其他Bean对象的。

BeanFactory和FactoryBean的区别

BeanFactory

Spring IoC容器的顶级对象,BeanFactory被翻译为“Bean工厂”,在Spring的IoC容器中,“Bean工厂”负责创建Bean对象。

BeanFactory是工厂。

FactoryBean

FactoryBean:它是一个Bean,是一个能够辅助Spring实例化其他Bean对象的一个Bean。

他是一个Bean


在Spring中,Bean可以分为两类:

  • 第一类:普通Bean
  • 第二类:工厂Bean

工厂Bean也是一种Bean,只不过这种Bean比较特殊,它可以辅助Spring实例化其他Bean对象。

注入自定义Date

众所周知 ,Date作为简单类用起来不简单,必须要规范那个b格式,其他格式是不会被识别的。

这种情况下,我们就可以使用FactoryBean来完成这个骚操作
编写DateFactoryBean实现FactoryBean接口:

package com.powernode.spring6.bean;import org.springframework.beans.factory.FactoryBean;import java.text.SimpleDateFormat;
import java.util.Date;public class DateFactoryBean implements FactoryBean<Date> {// 定义属性接收日期字符串private String date;// 通过构造方法给日期字符串属性赋值public DateFactoryBean(String date) {this.date = date;}@Overridepublic Date getObject() throws Exception {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.parse(this.date);}@Overridepublic Class<?> getObjectType() {return null;}
}

编写spring配置文件

<bean id="dateBean" class="com.powernode.spring6.bean.DateFactoryBean"><constructor-arg name="date" value="1999-10-11"/>
</bean><bean id="studentBean" class="com.powernode.spring6.bean.Student"><property name="birth" ref="dateBean"/>
</bean>
http://www.lryc.cn/news/106974.html

相关文章:

  • JVM-类加载器
  • ChatGPT在法律行业的市场潜力
  • Python编程从入门到实践练习第三章:列表简介
  • 【Spring Boot】请求参数传json数组,后端采用(pojo)新增案例(103)
  • Redis 持久化RDB和AOF
  • 【ThinkPHP】PHP实现分页功能
  • chrome 插件开发
  • 开源MinDoc wiki系统搭建
  • pytest.ini 文件说明
  • 遥感、GIS、GPS在土壤空间数据分析、适应性评价、制图、土壤普查中怎样应用?
  • git | git使用心得记录
  • java策略模式三种实现方案
  • VMWare虚拟系统上网设置及VMWare虚拟机三种工作模式详解
  • 计算机网络(3) --- 网络套接字TCP
  • 大数据技术之Hadoop(二)
  • 运维工程师第二阶段linux基础
  • ChatGPT安全技术
  • 使用cmd查看3568主板相关
  • SpringBoot限制(限流)接口访问频率
  • 蓝桥杯,我劝你不要参加的8个完美理由
  • ChatGPT及其工作原理;OpenAI申请注册商标GPT-5,引发关注
  • [C++项目] Boost文档 站内搜索引擎(2): 文档文本解析模块parser的实现、如何对文档文件去标签、如何获取文档标题...
  • 若依框架vue使用Element 如何把当前页面的所有Table表格row.id和一个表单的16个字段内容通过js传Java后台,Java后台是如何接收的
  • 迁移学习:使用Restnet预训练模型构建高效的水果识别模型
  • 浅谈机器视觉
  • 助力保险行业数字化创新,麒麟信安参展2023中国财险科技应用高峰论坛
  • eclipse was unable to locate its companion shared library
  • 【MySQL】使用C/C++连接MySQL数据库
  • 【Python】从同步到异步多核:测试桩性能优化,加速应用的开发和验证
  • 使用checkBox组件时,动态设置disabled,仍能触发click事件的原因及解决办法