Spring?Bean獲取方式的實例化方式詳解
Spring為Bean提供了多種實例化方式,通常包括4種方式。(也就是說在Spring中為Bean對象的創(chuàng)建準備了多種方案,目的是:更加靈活)
第一種:通過構造方法實例化
第二種:通過簡單工廠模式實例化
第三種:通過factory-bean實例化(工廠方法模式)
第四種:通過FactoryBean接口實例化
1.通過構造方法實例化
我們之前一直使用的就是這種方式!默認情況下,會調用Bean的無參數(shù)構造方法,這里在復習一遍!
SpringBean類
package com.bjpowernode.spring.bean;
public class SpringBean {
public SpringBean() {
System.out.println("SpringBean的無參數(shù)構造方法執(zhí)行了");
}
}spring.xml配置
第一種:在spring配置文件中直接配置類全路徑,Spring會自動調用該類的無參數(shù)構造方法來實例化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">
<!--Spring提供的實例化方式,第一種-->
<bean id="sb" class="com.bjpowernode.spring.bean.SpringBean"/>
</beans>BeanInstantiationTest測試類
package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.SpringBean;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
@Test
public void tesInstantiation1(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb);
}
}執(zhí)行結果:成功調用無參數(shù)構造方法實例化對象

2.通過簡單工廠模式實例化
簡單工廠模式又叫做靜態(tài)工廠方法模式,因為工廠類中有一個靜態(tài)方法!
第一步:定義一個Bean
package com.bjpowernode.spring.bean;
public class Vip {
public Vip() {
System.out.println("我是一個Vip");
}
}第二步:編寫簡單工廠模式當中的工廠類
package com.bjpowernode.spring.bean;
public class VipFactory {
// 里面有一個靜態(tài)方法
public static Vip get(){
// 實際上對象的創(chuàng)建還是我們程序員自己完成的
return new Vip();
}
}第三步:在Spring配置文件中指定創(chuàng)建該Bean的方法
第二種:通過簡單工廠模式。
需要在Spring配置文件中告訴Spring框架,調用哪個類的哪個方法獲取Bean?
①class屬性指定的是工廠類的全限定類名!
②factory-method屬性指定的是工廠類當中的靜態(tài)方法,也就是告訴Spring框架,調用這個方法可以獲取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">
<!--Spring提供的實例化方式,第二種-->
<bean id="vipBean" class="com.bjpowernode.spring.bean.VipFactory" factory-method="get"/>
</beans>第四步:編寫測試程序
package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.SpringBean;
import com.bjpowernode.spring.bean.Vip;
import com.bjpowernode.spring.bean.VipFactory;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
@Test
public void tesInstantiation2(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Object vipBean = applicationContext.getBean("vipBean", Vip.class);
System.out.println(vipBean);
}
}執(zhí)行結果:通過簡單工廠模式也能實例化對象

3.通過factory-bean實例化
本質上是:通過工廠方法模式進行實例化對象!
注:簡單工廠模式和工廠方法模式的區(qū)別
①簡單工廠模式是所有的產品對應一個工廠類,使用的是靜態(tài)方法!
②工廠方法模式是一個產品對應一個工廠類,使用的是實例方法!
第一步:定義一個Bean
package com.bjpowernode.spring.bean;
// 工廠方法模式當中的:具體產品角色
public class Gun {
public Gun() {
System.out.println("Gun的無參數(shù)構造方法執(zhí)行");
}
}第二步:定義具體工廠類,工廠類中定義實例方法
package com.bjpowernode.spring.bean;
// 工廠方法模式當中:的具體工廠角色
public class GunFactory {
// 實例方法
public Gun get(){
// 還是我們自己new的對象
return new Gun();
}
}第三步:在Spring配置文件中指定factory-bean以及factory-method
第三種:通過工廠方法模式。
通過 factory-bean屬性 + factory-method屬性來共同完成。告訴Spring框架,調用哪個對象(因為是實例方法需要創(chuàng)建對象)的哪個方法來獲取Bean。
①factory-bean屬性用來告訴Spring調用那個對象!
②factory-method屬性用來告訴Spring調用該對象的那個方法!
<?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提供的實例化方式,第三種-->
<bean id="gunBean" class="com.bjpowernode.spring.bean.GunFactory"/>
<bean id="gun" factory-bean="gunBean" factory-method="get"/>
</beans>第四步:編寫測試程序
package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.Gun;
import com.bjpowernode.spring.bean.SpringBean;
import com.bjpowernode.spring.bean.Vip;
import com.bjpowernode.spring.bean.VipFactory;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
@Test
public void tesInstantiation3(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Gun gun = applicationContext.getBean("gun", Gun.class);
System.out.println(gun);
}
}執(zhí)行結果:通過工廠方法模式也能實例化對象

4.通過FactoryBean接口實例化
①在第三種方式中,factory-bean和factory-method都是我們自己定義的。
②在Spring中,當編寫的類直接實現(xiàn)FactoryBean接口之后,factory-bean和factory-method就不需要指定了!factory-bean會自動指向實現(xiàn)FactoryBean接口的類,factory-method會自動指向getObject()方法!
第一步:定義一個Bean
package com.bjpowernode.spring.bean;
public class Person {
public Person() {
System.out.println("Person的無參數(shù)構造方法執(zhí)行了");
}
}第二步:編寫一個類實現(xiàn)FactoryBean接口,重寫里面的方法
PersonFactory也是一個Bean,只不過這個Bean比較特殊,叫做工廠Bean。通過工廠Bean這個特殊的Bean可以獲取一個普通的Bean!
package com.bjpowernode.spring.bean;
import org.springframework.beans.factory.FactoryBean;
public class PersonFactory implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
// 對象的創(chuàng)建也是自己new的
return new Person();
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
// 這個方法是默認存在的,true表示單例,false表示原型
return true;
}
}第三步:在Spring配置文件中配置FactoryBean
第四種:通過FactoryBean接口來實現(xiàn),這種方式實際上就是第三種方式的簡化!
①由于你編寫的類實現(xiàn)了FactoryBean接口,所以這個類是一個特殊的類,不需要你再手動指定:factory-bean、factory-method。 ②通過一個特殊的Bean:工廠Bean,來返回一個普通的Bean Person對象。即通過FactoryBean這個工廠Bean主要是想對普通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">
<!--Spring提供的實例化方式,第四種-->
<bean id="person" class="com.bjpowernode.spring.bean.PersonFactory" />
</beans>第四步:編寫測試程序
package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.*;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
@Test
public void tesInstantiation4(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Person person = applicationContext.getBean("person", Person.class);
System.out.println(person);
}
}執(zhí)行結果:通過FactoryBean接口實例化

注:FactoryBean在Spring中是一個接口,被稱為“工廠Bean”。“工廠Bean”是一種特殊的Bean,所有的“工廠Bean”都是用來協(xié)助Spring框架來創(chuàng)建其他Bean對象的!
5.BeanFactory和FactoryBean的區(qū)別-面試題
(1)BeanFactory(是一個工廠)
BeanFactory是Spring IoC容器的頂級對象,BeanFactory被翻譯為“Bean工廠”,在Spring的IoC容器中,“Bean工廠”負責創(chuàng)建Bean對象!
(2)FactoryBean(是一個Bean)
FactoryBean是一個Bean,是一個能夠輔助Spring實例化其它Bean對象的一個Bean!
在Spring中,Bean可以分為兩類:
- 第一類:普通Bean
- 第二類:工廠Bean(工廠Bean也是一種Bean,只不過這種Bean比較特殊,它可以輔助Spring實例化其它Bean對象)
6.使用FactoryBean注入自定義Date
①前面我們說過,java.util.Date在Spring中被當做簡單類型,簡單類型在注入的時候可以直接使用value屬性或value標簽來完成。
②但是之前我們已經測試過了,對于Date類型來說,采用value屬性或value標簽賦值的時候,對日期字符串的格式要求非常嚴格,必須是這種格式的:Mon Oct 10 14:30:26 CST 2022,其他格式是不會被識別的!
③當然我們也可以當成非簡單類型處理,使用ref屬性來處理,但是卻有一個弊端,獲取的都是當前的時間,并不能自己指定時間!
注:下面我們就使用FactoryBean來完成這個騷操作!
Student類
package com.bjpowernode.spring.bean;
import java.util.Date;
public class Student {
// 每個學生都有出生日期
private Date birth;
@Override
public String toString() {
return "Student{" +
"birth=" + birth +
'}';
}
public void setBirth(Date birth) {
this.birth = birth;
}
}編寫DateFactory實現(xiàn)FactoryBean接口
package com.bjpowernode.spring.bean;
import org.springframework.beans.factory.FactoryBean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFactory implements FactoryBean<Date> {
// 定義一個日期屬性,用來處理傳過來的日期字符串
private String date;
// 通過構造方法給日期字符串屬性賦值
public DateFactory(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;
}
}編寫spring配置文件
<?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">
<!--通過這個類的構造方法,把字符串轉換成Date-->
<bean id="date" class="com.bjpowernode.spring.bean.DateFactory">
<constructor-arg name="date" value="1999-01-14"/>
</bean>
<!--把上面的Date通過上面的類,使用ref屬性引進來-->
<bean id="studentBean" class="com.bjpowernode.spring.bean.Student">
<property name="birth" ref="date"/>
</bean>
</beans>編寫測試程序
package com.bjpowernode.spring.test;
import com.bjpowernode.spring.bean.*;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
@Test
public void testDate(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student studentBean = applicationContext.getBean("studentBean", Student.class);
System.out.println(studentBean);
}
}執(zhí)行結果

到此這篇關于Spring Bean獲取方式的實例化方式詳解的文章就介紹到這了,更多相關Spring Bean獲取方式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot環(huán)境屬性占位符解析和類型轉換方式
這篇文章主要介紹了SpringBoot環(huán)境屬性占位符解析和類型轉換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
MyBatis中?@Mapper?和?@MapperScan?的區(qū)別與使用解析
本文介紹了SpringBoot中MyBatis的兩個常用注解:@Mapper和@MapperScan,@Mapper用于標記單個Mapper接口,而@MapperScan用于批量掃描指定包下的所有Mapper接口,兩者都有各自適用的場景,選擇合適的注解可以提高開發(fā)效率并使代碼更加簡潔,感興趣的朋友一起看看吧2025-01-01
freemarker?jsp?java內存方式實現(xiàn)分頁示例
這篇文章主要介紹了freemarker?jsp?java內存方式實現(xiàn)分頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
解析ConcurrentHashMap: put方法源碼分析
ConcurrentHashMap是由Segment數(shù)組結構和HashEntry數(shù)組結構組成。Segment的結構和HashMap類似,是一種數(shù)組和鏈表結構,今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧2021-06-06

