新聞中心
SpringBean中怎么實(shí)現(xiàn)實(shí)例化,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
Bean的實(shí)例化
1.構(gòu)造器實(shí)例化:Spring容器通過Bean對應(yīng)類中默認(rèn)的無參構(gòu)造方法來實(shí)例化Bean
package com.itheima.instance.constructor; public class Bean1 { }
在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(); }}
定義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(); }}
首先配置了一個(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)容,你們掌握SpringBean中怎么實(shí)現(xiàn)實(shí)例化的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
名稱欄目:SpringBean中怎么實(shí)現(xiàn)實(shí)例化-創(chuàng)新互聯(lián)
文章地址:http://fisionsoft.com.cn/article/cepghd.html