您好,登錄后才能下訂單哦!
這篇“Spring Bean實(shí)例化方式怎么實(shí)現(xiàn)”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Spring Bean實(shí)例化方式怎么實(shí)現(xiàn)”文章吧。
第一種:通過構(gòu)造方法實(shí)例化
第二種:通過簡單工廠模式實(shí)例化
第三種:通過factory-bean實(shí)例化(工廠方法模式)
第四種:通過FactoryBean接口實(shí)例化
我們之前一直使用的就是這種方式!默認(rèn)情況下,會(huì)調(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會(huì)自動(dòng)調(diào)用該類的無參數(shù)構(gòu)造方法來實(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提供的實(shí)例化方式,第一種--> <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)造方法實(shí)例化對象
簡單工廠模式又叫做靜態(tài)工廠方法模式,因?yàn)楣S類中有一個(gè)靜態(tài)方法!
第一步:定義一個(gè)Bean
package com.bjpowernode.spring.bean; public class Vip { public Vip() { System.out.println("我是一個(gè)Vip"); } }
第二步:編寫簡單工廠模式當(dāng)中的工廠類
package com.bjpowernode.spring.bean; public class VipFactory { // 里面有一個(gè)靜態(tài)方法 public static Vip get(){ // 實(shí)際上對象的創(chuàng)建還是我們程序員自己完成的 return new Vip(); } }
第三步:在Spring配置文件中指定創(chuàng)建該Bean的方法
第二種:通過簡單工廠模式。
需要在Spring配置文件中告訴Spring框架,調(diào)用哪個(gè)類的哪個(gè)方法獲取Bean?
①class屬性指定的是工廠類的全限定類名!
②factory-method屬性指定的是工廠類當(dāng)中的靜態(tài)方法,也就是告訴Spring框架,調(diào)用這個(gè)方法可以獲取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提供的實(shí)例化方式,第二種--> <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é)果:通過簡單工廠模式也能實(shí)例化對象
本質(zhì)上是:通過工廠方法模式進(jìn)行實(shí)例化對象!
注:簡單工廠模式和工廠方法模式的區(qū)別
①簡單工廠模式是所有的產(chǎn)品對應(yīng)一個(gè)工廠類,使用的是靜態(tài)方法!
②工廠方法模式是一個(gè)產(chǎn)品對應(yīng)一個(gè)工廠類,使用的是實(shí)例方法!
第一步:定義一個(gè)Bean
package com.bjpowernode.spring.bean; // 工廠方法模式當(dāng)中的:具體產(chǎn)品角色 public class Gun { public Gun() { System.out.println("Gun的無參數(shù)構(gòu)造方法執(zhí)行"); } }
第二步:定義具體工廠類,工廠類中定義實(shí)例方法
package com.bjpowernode.spring.bean; // 工廠方法模式當(dāng)中:的具體工廠角色 public class GunFactory { // 實(shí)例方法 public Gun get(){ // 還是我們自己new的對象 return new Gun(); } }
第三步:在Spring配置文件中指定factory-bean以及factory-method
第三種:通過工廠方法模式。
通過 factory-bean屬性 + factory-method屬性來共同完成。告訴Spring框架,調(diào)用哪個(gè)對象(因?yàn)槭菍?shí)例方法需要?jiǎng)?chuàng)建對象)的哪個(gè)方法來獲取Bean。
①factory-bean屬性用來告訴Spring調(diào)用那個(gè)對象!
②factory-method屬性用來告訴Spring調(diào)用該對象的那個(gè)方法!
<?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提供的實(shí)例化方式,第三種--> <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é)果:通過工廠方法模式也能實(shí)例化對象
①在第三種方式中,factory-bean和factory-method都是我們自己定義的。
②在Spring中,當(dāng)編寫的類直接實(shí)現(xiàn)FactoryBean接口之后,factory-bean和factory-method就不需要指定了!factory-bean會(huì)自動(dòng)指向?qū)崿F(xiàn)FactoryBean接口的類,factory-method會(huì)自動(dòng)指向getObject()方法!
第一步:定義一個(gè)Bean
package com.bjpowernode.spring.bean; public class Person { public Person() { System.out.println("Person的無參數(shù)構(gòu)造方法執(zhí)行了"); } }
第二步:編寫一個(gè)類實(shí)現(xiàn)FactoryBean接口,重寫里面的方法
PersonFactory也是一個(gè)Bean,只不過這個(gè)Bean比較特殊,叫做工廠Bean。通過工廠Bean這個(gè)特殊的Bean可以獲取一個(gè)普通的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() { // 這個(gè)方法是默認(rèn)存在的,true表示單例,false表示原型 return true; } }
第三步:在Spring配置文件中配置FactoryBean
第四種:通過FactoryBean接口來實(shí)現(xiàn),這種方式實(shí)際上就是第三種方式的簡化!
①由于你編寫的類實(shí)現(xiàn)了FactoryBean接口,所以這個(gè)類是一個(gè)特殊的類,不需要你再手動(dòng)指定:factory-bean、factory-method。 ②通過一個(gè)特殊的Bean:工廠Bean,來返回一個(gè)普通的Bean Person對象。即通過FactoryBean這個(gè)工廠Bean主要是想對普通Bean進(jìn)行加工處理!
<?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提供的實(shí)例化方式,第四種--> <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接口實(shí)例化
注:FactoryBean在Spring中是一個(gè)接口,被稱為“工廠Bean”?!肮SBean”是一種特殊的Bean,所有的“工廠Bean”都是用來協(xié)助Spring框架來創(chuàng)建其他Bean對象的!
(1)BeanFactory(是一個(gè)工廠)
BeanFactory是Spring IoC容器的頂級對象,BeanFactory被翻譯為“Bean工廠”,在Spring的IoC容器中,“Bean工廠”負(fù)責(zé)創(chuàng)建Bean對象!
(2)FactoryBean(是一個(gè)Bean)
FactoryBean是一個(gè)Bean,是一個(gè)能夠輔助Spring實(shí)例化其它Bean對象的一個(gè)Bean!
在Spring中,Bean可以分為兩類:
第一類:普通Bean
第二類:工廠Bean(工廠Bean也是一種Bean,只不過這種Bean比較特殊,它可以輔助Spring實(shí)例化其它Bean對象)
①前面我們說過,java.util.Date在Spring中被當(dāng)做簡單類型,簡單類型在注入的時(shí)候可以直接使用value屬性或value標(biāo)簽來完成。
②但是之前我們已經(jīng)測試過了,對于Date類型來說,采用value屬性或value標(biāo)簽賦值的時(shí)候,對日期字符串的格式要求非常嚴(yán)格,必須是這種格式的:Mon Oct 10 14:30:26 CST 2022,其他格式是不會(huì)被識別的!
③當(dāng)然我們也可以當(dāng)成非簡單類型處理,使用ref屬性來處理,但是卻有一個(gè)弊端,獲取的都是當(dāng)前的時(shí)間,并不能自己指定時(shí)間!
注:下面我們就使用FactoryBean來完成這個(gè)騷操作!
Student類
package com.bjpowernode.spring.bean; import java.util.Date; public class Student { // 每個(gè)學(xué)生都有出生日期 private Date birth; @Override public String toString() { return "Student{" + "birth=" + birth + '}'; } public void setBirth(Date birth) { this.birth = birth; } }
編寫DateFactory實(shí)現(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> { // 定義一個(gè)日期屬性,用來處理傳過來的日期字符串 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è)類的構(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屬性引進(jìn)來--> <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實(shí)例化方式怎么實(shí)現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。