當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring的三种Bean的实例化方法
生活随笔
收集整理的這篇文章主要介紹了
Spring的三种Bean的实例化方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(本文內(nèi)容有網(wǎng)上+自己修改,如有錯(cuò)誤,請指正,謝謝。)
在spring中實(shí)例化bean的方式:
一、使用構(gòu)造器實(shí)例化;
這種實(shí)例化的方式可能在我們平時(shí)的開發(fā)中用到的是最多的,因?yàn)樵趚ml文件中配置簡單并且也不需要額外的工廠類來實(shí)現(xiàn)。
<Xml代碼>
<!--applicationContext.xml配置:-->
<bean id="personService" class="cn.mytest.service.impl.PersonServiceBean"></bean>
id是對象的名稱,class是要實(shí)例化的類,然后再通過正常的方式進(jìn)調(diào)用實(shí)例化的類即可,比如:
<Java代碼>
public void inSpring(){
//加載spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "/conf/applicationContext.xml"});
//調(diào)用getBean方法取得被實(shí)例化的對象。
PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService");
psb.save();
}
采用這種實(shí)例化方式要注意的是:要實(shí)例化的類中如果有構(gòu)造器的話,一定要有一個(gè)無參的構(gòu)造器。
二、使用靜態(tài)工廠方法實(shí)例化;
根據(jù)這個(gè)中實(shí)例化方法的名稱就可以知道要想通過這種方式進(jìn)行實(shí)例化就要具備兩個(gè)條件:
(一)、要有工廠類及其工廠方法;
(二)、工廠方法是靜態(tài)的。OK,知道這兩點(diǎn)就好辦了,首先創(chuàng)建工程類及其靜態(tài)方法:
<Java代碼>
package ....;
/**
*創(chuàng)建一個(gè)工廠類 PersonServiceFactory
*
*/
public class PersonServiceFactory {
//創(chuàng)建靜態(tài)方法
public static PersonServiceBean createPersonServiceBean(){
//返回實(shí)例化的類的對象
return new PersonServiceBean();
}
}
然后再去配置spring配置文件,配置的方法和上面有點(diǎn)不同,這里也是關(guān)鍵所在
<Xml代碼>
<!--applicationContext.xml配置:-->
<bean id="personService1" class="cn.mytest.service.impl.PersonServiceFactory" factory-method="createPersonServiceBean"></bean>
id是實(shí)例化的對象的名稱,class是工廠類,也就實(shí)現(xiàn)實(shí)例化類的靜態(tài)方法所屬的類,
factory-method是實(shí)現(xiàn)實(shí)例化類的靜態(tài)方法。
然后按照正常的調(diào)用方法去調(diào)用即可:
<Java代碼>
public void inSpring(){
//加載spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "/conf/applicationContext.xml"});
//調(diào)用getBean方法取得被實(shí)例化的對象。
PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService1");
psb.save();
}
三、使用實(shí)例化工廠方法實(shí)例化。
這個(gè)方法和上面的方法不同之處在與使用該實(shí)例化方式工廠方法不需要是靜態(tài)的,但是在spring的配置文件中需要配置更多的內(nèi)容,,首先創(chuàng)建工廠類及工廠方法:
<Java代碼>
package ...;
/**
*創(chuàng)建工廠類 PersonServiceFactory
*
*/
public class PersonServiceFactory {
//創(chuàng)建靜態(tài)方法
public PersonServiceBean createPersonServiceBean1(){
//返回實(shí)例化的類的對象
return new PersonServiceBean();
}
}
然后再去配置spring配置文件,配置的方法和上面有點(diǎn)不同,這里也是關(guān)鍵所在
<Xml代碼>
<!--applicationContext.xml配置:-->
<bean id="personServiceFactory" class="cn.mytest.service.impl.PersonServiceFactory"></bean>
<bean id="personService2" factory-bean="personServiceFactory" factory method="createPersonServiceBean1"></bean>
這里需要配置兩個(gè)bean,第一個(gè)bean使用的構(gòu)造器方法實(shí)例化工廠類,第二個(gè)bean中的id是實(shí)例化對象的名稱,factory-bean對應(yīng)的被實(shí)例化的工廠類的對象名稱,也就是第一個(gè)bean的id,factory-method是非靜態(tài)工廠方法。
然后按照正常的調(diào)用方法去調(diào)用即可:
<Java代碼>
public void instanceSpring(){
//加載spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "/conf/applicationContext.xml"});
//調(diào)用getBean方法取得被實(shí)例化的對象。
PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService2");
psb.save();
}
感謝 glzaction
在spring中實(shí)例化bean的方式:
一、使用構(gòu)造器實(shí)例化;
這種實(shí)例化的方式可能在我們平時(shí)的開發(fā)中用到的是最多的,因?yàn)樵趚ml文件中配置簡單并且也不需要額外的工廠類來實(shí)現(xiàn)。
<Xml代碼>
<!--applicationContext.xml配置:-->
<bean id="personService" class="cn.mytest.service.impl.PersonServiceBean"></bean>
id是對象的名稱,class是要實(shí)例化的類,然后再通過正常的方式進(jìn)調(diào)用實(shí)例化的類即可,比如:
<Java代碼>
public void inSpring(){
//加載spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "/conf/applicationContext.xml"});
//調(diào)用getBean方法取得被實(shí)例化的對象。
PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService");
psb.save();
}
采用這種實(shí)例化方式要注意的是:要實(shí)例化的類中如果有構(gòu)造器的話,一定要有一個(gè)無參的構(gòu)造器。
二、使用靜態(tài)工廠方法實(shí)例化;
根據(jù)這個(gè)中實(shí)例化方法的名稱就可以知道要想通過這種方式進(jìn)行實(shí)例化就要具備兩個(gè)條件:
(一)、要有工廠類及其工廠方法;
(二)、工廠方法是靜態(tài)的。OK,知道這兩點(diǎn)就好辦了,首先創(chuàng)建工程類及其靜態(tài)方法:
<Java代碼>
package ....;
/**
*創(chuàng)建一個(gè)工廠類 PersonServiceFactory
*
*/
public class PersonServiceFactory {
//創(chuàng)建靜態(tài)方法
public static PersonServiceBean createPersonServiceBean(){
//返回實(shí)例化的類的對象
return new PersonServiceBean();
}
}
然后再去配置spring配置文件,配置的方法和上面有點(diǎn)不同,這里也是關(guān)鍵所在
<Xml代碼>
<!--applicationContext.xml配置:-->
<bean id="personService1" class="cn.mytest.service.impl.PersonServiceFactory" factory-method="createPersonServiceBean"></bean>
id是實(shí)例化的對象的名稱,class是工廠類,也就實(shí)現(xiàn)實(shí)例化類的靜態(tài)方法所屬的類,
factory-method是實(shí)現(xiàn)實(shí)例化類的靜態(tài)方法。
然后按照正常的調(diào)用方法去調(diào)用即可:
<Java代碼>
public void inSpring(){
//加載spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "/conf/applicationContext.xml"});
//調(diào)用getBean方法取得被實(shí)例化的對象。
PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService1");
psb.save();
}
三、使用實(shí)例化工廠方法實(shí)例化。
這個(gè)方法和上面的方法不同之處在與使用該實(shí)例化方式工廠方法不需要是靜態(tài)的,但是在spring的配置文件中需要配置更多的內(nèi)容,,首先創(chuàng)建工廠類及工廠方法:
<Java代碼>
package ...;
/**
*創(chuàng)建工廠類 PersonServiceFactory
*
*/
public class PersonServiceFactory {
//創(chuàng)建靜態(tài)方法
public PersonServiceBean createPersonServiceBean1(){
//返回實(shí)例化的類的對象
return new PersonServiceBean();
}
}
然后再去配置spring配置文件,配置的方法和上面有點(diǎn)不同,這里也是關(guān)鍵所在
<Xml代碼>
<!--applicationContext.xml配置:-->
<bean id="personServiceFactory" class="cn.mytest.service.impl.PersonServiceFactory"></bean>
<bean id="personService2" factory-bean="personServiceFactory" factory method="createPersonServiceBean1"></bean>
這里需要配置兩個(gè)bean,第一個(gè)bean使用的構(gòu)造器方法實(shí)例化工廠類,第二個(gè)bean中的id是實(shí)例化對象的名稱,factory-bean對應(yīng)的被實(shí)例化的工廠類的對象名稱,也就是第一個(gè)bean的id,factory-method是非靜態(tài)工廠方法。
然后按照正常的調(diào)用方法去調(diào)用即可:
<Java代碼>
public void instanceSpring(){
//加載spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "/conf/applicationContext.xml"});
//調(diào)用getBean方法取得被實(shí)例化的對象。
PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService2");
psb.save();
}
感謝 glzaction
總結(jié)
以上是生活随笔為你收集整理的Spring的三种Bean的实例化方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工单管理系统otrs
- 下一篇: Spring六:Spring AOP A