Spring?Bean獲取方式的實例化方式詳解
Spring為Bean提供了多種實例化方式,通常包括4種方式。(也就是說在Spring中為Bean對象的創(chuàng)建準備了多種方案,目的是:更加靈活)
第一種:通過構(gòu)造方法實例化
第二種:通過簡單工廠模式實例化
第三種:通過factory-bean實例化(工廠方法模式)
第四種:通過FactoryBean接口實例化
1.通過構(gòu)造方法實例化
我們之前一直使用的就是這種方式!默認情況下,會調(diào)用Bean的無參數(shù)構(gòu)造方法,這里在復(fù)習(xí)一遍!
SpringBean類
package com.bjpowernode.spring.bean; public class SpringBean { public SpringBean() { System.out.println("SpringBean的無參數(shù)構(gòu)造方法執(zhí)行了"); } }
spring.xml配置
第一種:在spring配置文件中直接配置類全路徑,Spring會自動調(diào)用該類的無參數(shù)構(gòu)造方法來實例化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í)行結(jié)果:成功調(diào)用無參數(shù)構(gòu)造方法實例化對象
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框架,調(diào)用哪個類的哪個方法獲取Bean?
①class屬性指定的是工廠類的全限定類名!
②factory-method屬性指定的是工廠類當中的靜態(tài)方法,也就是告訴Spring框架,調(diào)用這個方法可以獲取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í)行結(jié)果:通過簡單工廠模式也能實例化對象
3.通過factory-bean實例化
本質(zhì)上是:通過工廠方法模式進行實例化對象!
注:簡單工廠模式和工廠方法模式的區(qū)別
①簡單工廠模式是所有的產(chǎn)品對應(yīng)一個工廠類,使用的是靜態(tài)方法!
②工廠方法模式是一個產(chǎn)品對應(yīng)一個工廠類,使用的是實例方法!
第一步:定義一個Bean
package com.bjpowernode.spring.bean; // 工廠方法模式當中的:具體產(chǎn)品角色 public class Gun { public Gun() { System.out.println("Gun的無參數(shù)構(gòu)造方法執(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框架,調(diào)用哪個對象(因為是實例方法需要創(chuàng)建對象)的哪個方法來獲取Bean。
①factory-bean屬性用來告訴Spring調(diào)用那個對象!
②factory-method屬性用來告訴Spring調(diào)用該對象的那個方法!
<?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í)行結(jié)果:通過工廠方法模式也能實例化對象
4.通過FactoryBean接口實例化
①在第三種方式中,factory-bean和factory-method都是我們自己定義的。
②在Spring中,當編寫的類直接實現(xiàn)FactoryBean接口之后,factory-bean和factory-method就不需要指定了!factory-bean會自動指向?qū)崿F(xiàn)FactoryBean接口的類,factory-method會自動指向getObject()方法!
第一步:定義一個Bean
package com.bjpowernode.spring.bean; public class Person { public Person() { System.out.println("Person的無參數(shù)構(gòu)造方法執(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í)行結(jié)果:通過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工廠”負責(zé)創(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標簽來完成。
②但是之前我們已經(jīng)測試過了,對于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 { // 每個學(xué)生都有出生日期 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; // 通過構(gòu)造方法給日期字符串屬性賦值 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"> <!--通過這個類的構(gòu)造方法,把字符串轉(zhuǎn)換成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í)行結(jié)果
到此這篇關(guān)于Spring Bean獲取方式的實例化方式詳解的文章就介紹到這了,更多相關(guān)Spring Bean獲取方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
老生常談Java?網(wǎng)絡(luò)編程?——?Socket?詳解
這篇文章主要介紹了Java?網(wǎng)絡(luò)編程?——?Socket?相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05java算法入門之有效的括號刪除有序數(shù)組中的重復(fù)項實現(xiàn)strStr
大家好,我是哪吒,一個熱愛編碼的Java工程師,本著"欲速則不達,欲達則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長,所謂成長,不過是用時間慢慢擦亮你的眼睛,少時看重的,年長后卻視若鴻毛,少時看輕的,年長后卻視若泰山,成長之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程2021-08-08SpringMVC使用hibernate-validator進行參數(shù)校驗最佳實踐記錄
這篇文章主要介紹了SpringMVC使用hibernate-validator進行參數(shù)校驗最佳實踐,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05Spring-Cloud-Function-Spel?漏洞環(huán)境搭建
這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡單,首先需要安裝maven jdk,具體安裝過程跟隨小編一起看看吧2022-03-03Java中的Set、List、Map的用法與區(qū)別介紹
這篇文章主要介紹了Java中的Set、List、Map的用法與區(qū)別,需要的朋友可以參考下2016-06-06Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解
這篇文章主要為大家介紹了Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08