最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBean中怎么實(shí)現(xiàn)實(shí)例化-創(chuàng)新互聯(lián)

SpringBean中怎么實(shí)現(xiàn)實(shí)例化,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),豐寧企業(yè)網(wǎng)站建設(shè),豐寧品牌網(wǎng)站建設(shè),網(wǎng)站定制,豐寧網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,豐寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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