Spring Bean實(shí)例化實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Bean實(shí)例化實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Bean的實(shí)例化
1.構(gòu)造器實(shí)例化:Spring容器通過Bean對應(yīng)類中默認(rèn)的無參構(gòu)造方法來實(shí)例化Bean
package com.itheima.instance.constructor; public class Bean1 { }
<?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="bean1" class="com.itheima.instance.constructor.Bean1"></bean> </beans>
在beans1.xml文件中,定義了一個(gè)id為bean1的Bean,并通過class屬性指定其對應(yīng)的實(shí)現(xiàn)類Bean1
package com.itheima.instance.constructor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest1 { public static void main(String[] args) { //定義配置文件路徑 String xmlPath = "com/itheima/instance/constructor/beans1.xml"; //ApplicationContext在加載配置文件時(shí),對Bean進(jìn)行實(shí)例化 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Bean1 bean = (Bean1) applicationContext.getBean("bean1"); System.out.println(bean); } }
在InstanceTest1類中,首先定義了配置文件的路徑,然后Spring容器ApplicationContext會加載配置文件。在加載時(shí),Spring容器會通過id為bean1的實(shí)現(xiàn)類Bean1中默認(rèn)的無參構(gòu)造方法對Bean進(jìn)行實(shí)例化。
2. 靜態(tài)工廠方法實(shí)例化
package com.itheima.instance.static_factory; public class Bean2 { }
package com.itheima.instance.static_factory; public class MyBean2Factory { //使用自己的方法創(chuàng)建Bean2實(shí)例 public static Bean2 createBean(){ return new Bean2(); } }
<?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="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean"></bean> </beans>
定義id為bean2的Bean,通過class屬性指定其對應(yīng)的工廠實(shí)現(xiàn)類(MyBean2Factory.java),需要增加factory-method屬性來告訴Spring容器其方法名稱為createBean。
package com.itheima.instance.static_factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest2 { public static void main(String[] args) { //定義配置文件路徑 String xmlPath = "com/itheima/instance/static_factory/beans2.xml"; //ApplicationContext在加載配置文件時(shí),對Bean進(jìn)行實(shí)例化 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean2")); } }
3.實(shí)例工廠方式實(shí)例化
package com.itheima.instance.factory; public class Bean3 { }
package com.itheima.instance.factory; public class MyBean3Factory { public MyBean3Factory(){ System.out.println("bean3工廠實(shí)例化中"); } //創(chuàng)建Bean3實(shí)例的方法 public Bean3 createBean(){ return new Bean3(); } }
<?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="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory"></bean> <!-- 使用 factory-bean屬性指向配置的實(shí)例工廠 使用factory-method屬性確定使用工廠中的哪個(gè)方法 --> <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"></bean> </beans>
首先配置了一個(gè)工廠Bean,然后配置了需要實(shí)例化的Bean。在id為bean3的Bean中,使用factory-bean屬性指向配置的實(shí)例工廠,使用factory-method屬性來確定使用工廠中的createBean()方法
package com.itheima.instance.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest3 { public static void main(String[] args) { //指定配置文件路徑 String xmlPath = "com/itheima/instance/factory/beans3.xml"; //ApplicationContext加載配置文件時(shí),對Bean進(jìn)行實(shí)例化 @SuppressWarnings("resource") ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean3")); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Iterator迭代器遍歷集合數(shù)據(jù)的方法小結(jié)
這篇文章主要介紹了Java使用Iterator迭代器遍歷集合數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了java迭代器進(jìn)行集合數(shù)據(jù)遍歷的常見操作技巧,需要的朋友可以參考下2019-11-11java實(shí)現(xiàn)計(jì)算器加法小程序(圖形化界面)
這篇文章主要介紹了Java實(shí)現(xiàn)圖形化界面的計(jì)算器加法小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05MyBatis-Plus UpdateWrapper 使用常見陷阱和解決方案
MyBatis-Plus是Mybatis的一個(gè)增強(qiáng),簡化了Mybatis的開發(fā)過程,不僅保持了Mybatis原有的功能,而且在無代碼侵略下增加了許多的增強(qiáng)的功能,提供了豐富的CRUD操作,單表的CRUD操作無需編寫SQL語句,本文介紹的是UpdateWrapper的常見陷阱和對應(yīng)的解決方案,感興趣的朋友一起看看吧2024-08-08

淺談Servlet 實(shí)現(xiàn)網(wǎng)頁重定向的方法

Java利用redis zset實(shí)現(xiàn)延時(shí)任務(wù)詳解