七、Bean的实例化方式
创始人
2024-05-28 12:13:42
0

Spring为Bean提供了多种实例化方式,通常包括4种方式。(也就是说在Spring中为Bean对象的创建准备了多种方案,目的是:更加灵活)
  • 第一种:通过构造方法实例化
  • 第二种:通过简单工厂模式实例化
  • 第三种:通过factory-bean实例化
  • 第四种:通过FactoryBean接口实例化

1.通过构造方法实例化

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

定义一个Bean

package com.powernode.spring6.bean;public class SpringBean {public SpringBean() {System.out.println("SpringBean的无参数构造方法执行");}
}

Spring配置文件




测试

package com.powernode.spring6.test;import com.powernode.spring6.bean.SpringBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {@Testpublic void testInstantiation1(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);System.out.println(springBean);}
}

2.通过简单工厂模式实例化

定义一个Bean

package com.powernode.spring6.bean;
/*** Bean*/
public class Star {public Star() {System.out.println("Star的无参数构造方法执行");}
}

编写简单工厂模式当中的工厂类

package com.powernode.spring6.bean;
/*** 简单工厂模式中的工厂类角色*/
public class StarFactory {// 工厂类中有一个静态方法public static Star get(){// Star对象最终实际上创建的时候还是我们负责new的对象return new Star();}
}

在Spring配置文件中指定创建该Bean的方法(使用factory-method属性指定)




编写测试程序

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

3.通过factory-bean实例化

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

定义一个Bean

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

定义具体工厂类,工厂类中定义实例方法

package com.powernode.spring6.bean;
/*** 工厂方法模式中的 具体工厂角色*/
public class GunFactory {// 工厂方法模式中的具体工厂角色中的方法是:实例方法public Gun get(){// 实际上new对象还是我们自己new的return new Gun();}
}

在Spring配置文件中指定factory-bean以及factory-method






编写测试程序

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

4.通过FactoryBean接口实例化

以上的第三种方式中,factory-bean是我们自定义的,factory-method也是我们自己定义的。

在Spring中,当你编写的类直接实现FactoryBean接口之后,factory-bean不需要指定了,factory-method也不需要指定了。
factory-bean会自动指向实现FactoryBean接口的类,factory-method会自动指向getObject()方法。

定义一个Bean

package com.powernode.spring6.bean;
/*** 普通的Bean*/
public class Person {public Person() {System.out.println("Person的无参数构造方法执行");}
}

编写一个类实现FactoryBean接口

package com.powernode.spring6.bean;import org.springframework.beans.factory.FactoryBean;public class PersonFactoryBean implements FactoryBean {// PersonFactoryBean也是一个Bean,这个Bean比较特殊,叫做工厂Bean// 通过工厂Bean可以获取普通的Bean@Overridepublic Person getObject() throws Exception {// 最终这个Bean的创建还是我们自己new的return new Person();}@Overridepublic Class getObjectType() {return null;}/*** 这个方法在接口中有默认实现* 默认返回true,表示单例的* 返回false,表示多例的* @return*/@Overridepublic boolean isSingleton() {return FactoryBean.super.isSingleton();}
}

在Spring配置文件中配置FactoryBean







测试程序:

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

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

5.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对象。)

6 注入自定义Date

java.util.Date在Spring中被当做简单类型,简单类型在注入的时候可以直接使用value属性或value标签来完成。但对于Date类型来说,采用value属性或value标签赋值的时候,对日期字符串的格式要求非常严格,必须是这种格式的:Mon Oct 10 14:30:26 CST 2022。其他格式是不会被识别的。

package com.powernode.spring6.bean;import java.util.Date;/*** 普通的Bean*/
public class Student {// java.util.Date 在Spring当中被当做简单类型,注入日期字符串格式有要求// java.util.Date 在Spring当中也可以被当做非简单类型private Date birth;public void setBirth(Date birth) {this.birth = birth;}@Overridepublic String toString() {return "Student{" +"birth=" + birth +'}';}
}

编写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 {// 定义属性接收日期字符串private String strDate;// 通过构造方法给日期字符串属性赋值public DateFactoryBean(String strDate) {this.strDate = strDate;}@Overridepublic Date getObject() throws Exception {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date date = sdf.parse(strDate);return date;}@Overridepublic Class getObjectType() {return null;}
}

在Spring配置文件中配置






测试:

@Test
public void testDate(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");Student student = applicationContext.getBean("student", Student.class);System.out.println(student);
}

相关内容

热门资讯

学生读书交流会主持词 学生读书交流会主持词  主持词要把握好吸引观众、导入主题、创设情境等环节以吸引观众。在当下的中国社会...
晚会的闭幕词 晚会的闭幕词(精选16篇)  主持词是主持活动的必备稿子,是活跃气氛,引导活动进行的存在,下面是小编...
阿甘正传电影经典台词 阿甘正传电影经典台词大全  《阿甘正传》给我们展现了一个虽然智商只有75,却是忠诚、守信、执着、友善...
致青春经典台词 致青春经典台词  1、青春是有限的,不能在犹豫和观望中度过。  2、很多东西就像气球一样,看上去很美...
追悼会主持词 追悼会主持词  什么是追悼会  追悼会,为悼念死者而召开的会议。有些在死者遗体所在地举行,有些在殡仪...
幼儿园大班新年联欢会主持词   主持人:左XX  开场:  左:亲爱的老师、同学们:  合:大家好!  彭:20XX年马上就要过...
新春的主持稿 新春的主持稿  在日常生活和工作中,需要使用主持稿的情况越来越多,主持稿是主持人在会议或是节目当中串...
五四青年节的致辞 五四青年节的致辞(通用20篇)  在平日的学习、工作和生活里,大家总少不了要接触或使用致辞吧,致辞是...
新年年会简短优秀致辞 新年年会简短优秀致辞(通用8篇)  在生活、工作和学习中,大家都不可避免地会接触到致辞吧,致辞具有很...
告别仪式的主持词 告别仪式的主持词3篇  告别主持词篇一:  男:离别,是一个沉重的动词。  女:离别,一个让人一生难...
喜爱夜蒲2经典台词 喜爱夜蒲2经典台词  1、做要做到最好,玩要玩到最尽。  2、我们明知不能相爱,可还是相爱了,未曾绽...
领导年会致辞 领导年会致辞  无论在学习、工作或是生活中,大家对致辞都不陌生吧,致辞是指在举行会议或某种仪式时具有...
关于保险公司年会主持词 关于保险公司年会主持词  公司的类型有哪些  根据《中华人民共和国公司法》公司的主要形式为无限责任公...
在年会上的致辞 在年会上的致辞范文(精选5篇)  在平时的学习、工作或生活中,大家都写过致辞吧,致辞是指在举行会议或...
企业开工仪式致辞 企业开工仪式致辞(精选7篇)  在平时的学习、工作或生活中,大家都不可避免地会接触到致辞吧,致辞是指...
小学第二学期的开学典礼主持词 小学第二学期的开学典礼主持词  主持词已成为各种演出活动和集会中不可或缺的一部分。在当今中国社会,各...
升学宴主持词 升学宴主持词3篇  高考过后考生及家长需要考虑举办升学宴会酒席的事宜了,升学宴主持词怎么写?下面是小...
同学聚会致辞 同学聚会致辞(精选6篇)  在生活、工作和学习中,大家都尝试过写致辞吧,在各种重大的庆典、外交、纪念...
升学宴会主持词 升学宴会主持词9篇  主持人在台上表演的灵魂就表现在主持词中。在当今社会中,各种场合中活跃现场气氛的...