文章目录
Spring6 -(08)Bean的实例化方法
1. Bean的实例化方法
Spring为Bean提供了多种实例化方式,通常包括4种方式。(也就是说在Spring中为Bean对象的创建准备了多种方案,目的是:更加灵活)
- 第一种:通过构造方法实例化
- 第二种:通过简单工厂模式实例化
- 第三种:通过factory-bean实例化
- 第四种:通过FactoryBean接口实例化
2. 通过构造方法实例化
在Spring配置文件中直接配置类全路径,Spring会自动调用该类的无参构造方法来实例化Bean
第一步:创建User类
package com.julissa.spring.bean;
/**
* bean类
*/
public class User {
}
第二步:配置beans.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">
<!--在Spring配置文件中直接配置类全路径,Spring会自动调用该类的无参构造方法来实例化Bean-->
<bean id="user" class="com.julissa.spring.bean.User"/>
</beans>
第三步:编写测试程序
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
第四步:运行测试程序
3. 通过简单工厂模式实例化
第一步:创建Star类
package com.julissa.spring.bean;
/**
* bean类
*/
public class Star {
public Star() {
System.out.println("Star的无参构造方法执行了~~~");
}
}
第二步:编写简单工厂模式当中的工厂类
package com.julissa.spring.bean;
/**
*
* 简单工厂模式中的工厂类角色
*/
public class StarFactory {
//静态工厂模式
public static Star getStar(){
//这个star对象最终实际上创建的时候还是我们负责new的对象
return new Star();
}
}
第三步:配置beans.xml
在Spring配置文件中指定创建该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配置文件中告诉Spring框架,调用哪个类中的哪个方法获取bean-->
<!-- factory-method 属性指定的是简单工厂中的静态方法-->
<bean id="star" class="com.julissa.spring.bean.StarFactory" factory-method="getStar"/>
</beans>
第四步:编写测试程序
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Star star = applicationContext.getBean("star", Star.class);
System.out.println(star);
}
第五步:运行测试程序
4. 通过factory-bean实例化
这种方式本质上是:通过工厂方法模式进行实例化。
第一步:创建Star类
package com.julissa.spring.bean;
/**
* bean类
*/
public class Star {
public Star() {
System.out.println("Star的无参构造方法执行了~~~");
}
}
第二步:编写工厂方法模式当中的工厂类
package com.julissa.spring.bean;
/**
*
* 工厂模式中的工厂类角色
*/
public class StarFactory {
//实例方法
public Star getStar(){
//这个star对象最终实际上创建的时候还是我们负责new的对象
return new Star();
}
}
第三步:配置beans.xml
使用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">
<!--
通过工厂方法模式,使用factory-bean和factory-method属性共同完成,
需要在Spring配置文件中告诉Spring框架,调用哪个类中的哪个方法获取bean
-->
<bean id="starFactory" class="com.julissa.spring.bean.StarFactory"/>
<bean id="star" factory-bean="starFactory" factory-method="getStar"/>
</beans>
第四步:编写测试程序
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Star star = applicationContext.getBean("star", Star.class);
System.out.println(star);
}
第五步:运行测试程序
5. 通过FactoryBean接口实例化
第一步:创建Phone类
package com.julissa.spring.bean;
/**
* 具体角色类
*/
public class Phone {
public Phone() {
System.out.println("Phone的无参构造方法执行了!");
}
}
第二步:编写一个类实现FactoryBean接口
package com.julissa.spring.bean;
import org.springframework.beans.factory.FactoryBean;
public class PhoneFactory implements FactoryBean<Phone> {
@Override
public Phone getObject() throws Exception {
//返回的Bean对象在实例化的时候也是我们负责new的
return new Phone();
}
@Override
public Class<?> getObjectType() {
return null;
}
/**
* 这个接口在方法中有默认实现
* 默认返回true,表示单例的
* 如果想要多例,直接将方法修改为return false即可
* @return
*/
@Override
public boolean isSingleton() {
return true;
}
}
第三步:配置beans.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">
<!-- 通过FactoryBean接口实现 -->
<!-- 这种写法实际上是第三中写法的简化-->
<!-- 通过一个特殊的工厂bean,来返回一个普通的Bean对象-->
<bean id="phoneBean" class="com.julissa.spring.bean.PhoneFactory"/>
</beans>
第四步:编写测试程序
@Test
public void test3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Phone phone = applicationContext.getBean("phoneBean", Phone.class);
System.out.println(phone);
}
第五步:运行测试程序
6. BeanFactory和FactoryBean的区别
1.BeanFactory
Spring IoC容器的顶级对象,BeanFactory被翻译为“Bean工厂”,在Spring的IoC容器中,“Bean工厂”负责创建Bean对象。
BeanFactory是工厂。
2.FactoryBean
FactoryBean:它是一个Bean,是一个能够辅助Spring实例化其它Bean对象的一个Bean。
在Spring中,Bean可以分为两类:
- 第一类:普通Bean
- 第二类:工厂Bean(记住:工厂Bean也是一种Bean,只不过这种Bean比较特殊,它可以辅助Spring实例化其它Bean对象。)
7. 注入自定义Date
java.util.Date在Spring中被当做简单类型,简单类型在注入的时候可以直接使用value属性或value标签来完成。
但我们之前已经测试过了,对于Date类型来说,采用value属性或value标签赋值的时候,对日期字符串的格式要求非常严格,必须是这种格式的:Mon Oct 10 14:30:26 CST 2022。其他格式是不会被识别的。
如以下代码:
第一步:创建DateBean类
package com.julissa.spring.bean;
import java.util.Date;
public class DateBean {
private Date date;
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "DateBean{" +
"date=" + date +
'}';
}
}
第二步:配置beans.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="dateBean" class="com.julissa.spring.bean.DateBean">
<property name="date" value="Mon Oct 10 14:30:26 CST 2002"/>
</bean>
</beans>
第三步:编写测试程序
@Test
public void test4(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
DateBean dateBean = applicationContext.getBean("dateBean", DateBean.class);
System.out.println(dateBean);
}
第四步:运行测试程序
如果把日期格式修改一下:
第五步:修改beans.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="dateBean" class="com.julissa.spring.bean.DateBean">
<property name="date" value="2002-10-10"/>
</bean>
</beans>
第六步:运行测试程序
这种情况下,我们就可以使用FactoryBean来完成这个骚操作。
第七步:编写DateFactoryBean实现FactoryBean接口
package com.julissa.spring.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;
}
@Override
public Date getObject() throws Exception {
//格式化字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(this.date);
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
第八步:配置beans.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">
<!-- 配置DateFactoryBean -->
<bean id="dateFactoryBean" class="com.julissa.spring.bean.DateFactoryBean">
<constructor-arg name="date" value="2000-10-10"/>
</bean>
<!-- 配置DateBean -->
<bean id="dateBean" class="com.julissa.spring.bean.DateBean">
<property name="date" ref="dateFactoryBean"/>
</bean>
</beans>
第九步:运行测试程序