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);
}
通过简单工厂模式实例化
- 定义一个Bean
package com.powernode.spring6.bean;public class Vip {
}
- 编写简单工厂模式当中的工厂类
package com.powernode.spring6.bean;public class VipFactory {public static Vip get(){return new Vip();}
}
- 在Spring配置文件中指定创建该Bean的方法(使用factory-method属性指定)
<bean id="vipBean" class="com.powernode.spring6.bean.VipFactory" factory-method="get"/>
- 编写测试程序
@Test
public void testSimpleFactory(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");Vip vip = applicationContext.getBean("vipBean", Vip.class);System.out.println(vip);
}
通过factory-bean实例化
本质上是通过工厂方法模式进行实例化
- 定义一个Bean
package com.powernode.spring6.bean;public class Order {
}
- 定义具体工厂类,工厂类中定义实例方法
package com.powernode.spring6.bean;public class OrderFactory {public Order get(){return new Order();}
}
- 在Spring配置文件中指定factory-bean以及factory-method
<bean id="orderFactory" class="com.powernode.spring6.bean.OrderFactory"/>
<bean id="orderBean" factory-bean="orderFactory" factory-method="get"/>
- 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>