目錄
一、對象的創(chuàng)建方式
1. 使用構(gòu)造方法
2. 使用工廠類方法
3. 使用工廠類的靜態(tài)方法
二、對象的創(chuàng)建策略
1. 單例策略
2. 多例策略
三、對象的銷毀時(shí)機(jī)
四、生命周期方法
1. 定義生命周期方法
2. 配置生命周期方法
3. 測試
五、獲取Bean對象的方式
1. 通過id/name獲取
2. 通過類型獲取
3. 通過類型+id/name獲取
往期專欄&文章相關(guān)導(dǎo)讀?
一、對象的創(chuàng)建方式
Spring會(huì)幫助我們創(chuàng)建bean,那么它底層是調(diào)用什么方法進(jìn)行創(chuàng)建的呢?有以下三種方法
- 使用構(gòu)造方法
- 使用工廠類方法
- 使用工廠類的靜態(tài)方法
接下來詳細(xì)講解這三種方法。
1. 使用構(gòu)造方法
????????Spring默認(rèn)使用類的空參構(gòu)造方法創(chuàng)建bean,假如類沒有空參構(gòu)造方法,將無法完成bean的創(chuàng)建,接下來我們可以測試一下。
package com.example.dao;
import com.example.pojo.Student;
public class StudentDaoImpl1 implements StudentDao{
/*public StudentDaoImpl1() {
}*/
public StudentDaoImpl1(int a){};
@Override
public Student findById(int id){
return new Student(id,"程序員","北京");
}
}
錯(cuò)誤原因:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentDao' defined in class path resource [bean.xml]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'int' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
翻譯:上下文初始化過程中遇到異常-取消刷新嘗試:org.springframework.beans.factory.UnsatisfiedDependencyException:創(chuàng)建類路徑資源[bean.xml]中定義的名稱為“studentDao”的bean時(shí)出錯(cuò):通過構(gòu)造函數(shù)參數(shù)0表示的不滿足依賴關(guān)系;嵌套異常為org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有可用的類型為“int”的符合條件的bean:應(yīng)至少有1個(gè)符合自動(dòng)連線候選條件的bean。依賴項(xiàng)注釋:{}
其實(shí)就是沒有空的構(gòu)造函數(shù),加上一個(gè)就好了
2. 使用工廠類方法
????????Spring可以調(diào)用工廠類的方法創(chuàng)建bean:創(chuàng)建工廠類,工廠類提供創(chuàng)建對象的方法,在配置文件中配置創(chuàng)建bean的方式為工廠方式。
工廠類StudentDaoFactory:
package com.example.dao;
public class StudentDaoFactory {
public StudentDao getStudentDao(){
return new StudentDaoImpl1(1);
}
}
bean.xml的配置:
<!-- id: 工廠對象的id,class:工廠類 -->
<bean id="studentDaoFactory" class="com.example.dao.StudentDaoFactory" />
<!-- id:bean對象的id,factory-bean:工廠對象的id,factory-method:工廠方法 -->
<bean id="studentDao" factory-bean="studentDaoFactory" factory-method="getStudentDao"></bean>
測試方法:?
@Test
public void t2(){
// 創(chuàng)建spring容器
ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\JavaProjects\\06SSM_Projects\\springdemo\\spring_ioc1\\src\\main\\resources\\bean.xml");
// 從容器中獲取對象
StudentDao userDao = ac.getBean("studentDao",StudentDao.class);
System.out.println(userDao);
System.out.println(userDao.findById(1));;
}
測試結(jié)果:?
OK,確實(shí)成功寫出來了?
3. 使用工廠類的靜態(tài)方法
????????Spring可以調(diào)用工廠類的靜態(tài)方法創(chuàng)建bean,創(chuàng)建工廠類,工廠提供創(chuàng)建對象的靜態(tài)方法,在配置文件中配置創(chuàng)建bean的方式為工廠靜態(tài)方法。
工廠類StudentDaoFactory2
package com.example.dao;
public class StudentDaoFactory2 {
public static StudentDao getStudentDao2() {
return new StudentDaoImpl2();
}
}
bean.xml的配置?
<!-- id:bean的id class:工廠全類名 factory-method:工廠靜態(tài)方法 -->
<bean id="studentDao" class="com.example.dao.StudentDaoFactory2" factory-method="getStudentDao2"/>
都是可以成功運(yùn)行的。?
二、對象的創(chuàng)建策略
? ? ? ? scope屬性設(shè)置對象的創(chuàng)建策略。Spring通過配置 <bean> 中的 scope 屬性設(shè)置對象的創(chuàng)建策略,共有兩種種創(chuàng)建策略。
1. 單例策略
????????singleton:單例,默認(rèn)策略。整個(gè)項(xiàng)目只會(huì)創(chuàng)建一個(gè)對象,通過 <bean> 中的 lazy-init 屬性可以設(shè)置單例對象的創(chuàng)建時(shí)機(jī):lazy-init="false"(默認(rèn)):立即創(chuàng)建,在容器啟動(dòng)時(shí)會(huì)創(chuàng)建配置文件中的所有Bean對象。lazy-init="true":延遲創(chuàng)建,第一次使用Bean對象時(shí)才會(huì)創(chuàng)建。下面測試獲取對象后的哈希值是否一樣就可以知道是否配置單例策略了
bean.xml的配置?
<bean id="studentDao" class="com.example.dao.StudentDaoImpl2" scope="singleton" lazy-init="true" />
測試方法?
@Test
public void t3(){
// 創(chuàng)建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 從容器獲取對象
StudentDao studentDao1 = ac.getBean("studentDao",StudentDao.class);
StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);
StudentDao studentDao3 = ac.getBean("studentDao",StudentDao.class);
System.out.println(studentDao1.hashCode());
System.out.println(studentDao2.hashCode());
System.out.println(studentDao3.hashCode());
}
運(yùn)行結(jié)果?
????????OK,得到的對象都是同一個(gè)哈希值,說明確實(shí)是同一個(gè)對象也就是說成功配置了單例模式。?
2. 多例策略
prototype:多例,每次從容器中獲取時(shí)都會(huì)創(chuàng)建對象。
bean.xml配置?
<!-- 配置多例策略 -->
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2" scope="prototype"></bean>
測試結(jié)果?
得到的哈希值不一樣,說明得到的是不同的對象,確實(shí)是多例策略?。
request:每次請求創(chuàng)建一個(gè)對象,只在web環(huán)境有效。
session:每次會(huì)話創(chuàng)建一個(gè)對象,只在web環(huán)境有效。
gloabal-session:一次集群環(huán)境的會(huì)話創(chuàng)建一個(gè)對象,只在web環(huán)境有效。
三、對象的銷毀時(shí)機(jī)
對象的創(chuàng)建策略不同,銷毀時(shí)機(jī)也不同:
- singleton:對象隨著容器的銷毀而銷毀。
- prototype:使用JAVA垃圾回收機(jī)制銷毀對象。
- request:當(dāng)處理請求結(jié)束,bean實(shí)例將被銷毀。
- session:當(dāng)HTTP Session最終被廢棄的時(shí)候,bean也會(huì)被銷毀掉。
- gloabal-session:集群環(huán)境下的session銷毀,bean實(shí)例也將被銷毀。
四、生命周期方法
????????Bean對象的生命周期包含創(chuàng)建——使用——銷毀,Spring可以配置Bean對象在創(chuàng)建和銷毀時(shí)自動(dòng)執(zhí)行的方法:
1. 定義生命周期方法
在StudentDaoImpl2中新增生命周期方法
// 創(chuàng)建時(shí)自動(dòng)執(zhí)行的方法
public void init(){
System.out.println("使用StudentDaoImpl2創(chuàng)建對象"+this.hashCode());
}
// 銷毀時(shí)自動(dòng)執(zhí)行的方法
public void destroy(){
System.out.println("銷毀StudentDaoImpl2創(chuàng)建的對象"+this.hashCode());
}
2. 配置生命周期方法
<!-- init-method:創(chuàng)建對象時(shí)執(zhí)行的方法 destroy-method:銷毀對象時(shí)執(zhí)行的方法 -->
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2" scope="singleton" init-method="init" destroy-method="destory"></bean>
3. 測試
測試方法
@Test
public void t3(){
??// 創(chuàng)建Spring容器
??ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
??// 銷毀Spring容器,ClassPathXmlApplicationContext才有銷毀容器的方法
??ac.close();
}
測試結(jié)果?
也確實(shí)可以?
五、獲取Bean對象的方式
1. 通過id/name獲取
獲取對象的時(shí)候是這樣:
StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
2. 通過類型獲取
獲取對象的時(shí)候是這樣:
StudentDao studentDao2 = ac.getBean(StudentDao.class);
不需要進(jìn)行類型強(qiáng)轉(zhuǎn)
3. 通過類型+id/name獲取
????????雖然使用類型獲取不需要強(qiáng)轉(zhuǎn),但如果在容器中有一個(gè)接口的多個(gè)實(shí)現(xiàn)類對象,則獲取時(shí)會(huì)報(bào)錯(cuò),此時(shí)需要使用類型+id/name獲取,獲取對象是這樣:文章來源:http://www.zghlxwxcb.cn/news/detail-427591.html
StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);
往期專欄&文章相關(guān)導(dǎo)讀?
?????大家如果對于本期內(nèi)容有什么不了解的話也可以去看看往期的內(nèi)容,下面列出了博主往期精心制作的Maven,Mybatis等專欄系列文章,走過路過不要錯(cuò)過哎!如果對您有所幫助的話就點(diǎn)點(diǎn)贊,收藏一下啪。其中Spring專欄有些正在更,所以無法查看,但是當(dāng)博主全部更完之后就可以看啦。文章來源地址http://www.zghlxwxcb.cn/news/detail-427591.html
Maven系列專欄 | Maven工程開發(fā) |
Maven聚合開發(fā)【實(shí)例詳解---5555字】 |
Mybatis系列專欄 | MyBatis入門配置 |
Mybatis入門案例【超詳細(xì)】 | |
MyBatis配置文件 —— 相關(guān)標(biāo)簽詳解 | |
Mybatis模糊查詢——三種定義參數(shù)方法和聚合查詢、主鍵回填 | |
Mybatis動(dòng)態(tài)SQL查詢 --(附實(shí)戰(zhàn)案例--8888個(gè)字--88質(zhì)量分) | |
Mybatis分頁查詢——四種傳參方式 | |
Mybatis一級緩存和二級緩存(帶測試方法) | |
Mybatis分解式查詢 | |
Mybatis關(guān)聯(lián)查詢【附實(shí)戰(zhàn)案例】 | |
MyBatis注解開發(fā)---實(shí)現(xiàn)增刪查改和動(dòng)態(tài)SQL | |
MyBatis注解開發(fā)---實(shí)現(xiàn)自定義映射關(guān)系和關(guān)聯(lián)查詢 |
Spring系列專欄 | Spring IOC 入門簡介【自定義容器實(shí)例】 |
IOC使用Spring實(shí)現(xiàn)附實(shí)例詳解 | |
Spring IOC之對象的創(chuàng)建方式、策略及銷毀時(shí)機(jī)和生命周期且獲取方式 | |
Spring DI簡介及依賴注入方式和依賴注入類型 | |
Spring IOC相關(guān)注解運(yùn)用——上篇 | |
Spring IOC相關(guān)注解運(yùn)用——下篇 | |
Spring AOP簡介及相關(guān)案例 | |
注解、原生Spring、SchemaBased三種方式實(shí)現(xiàn)AOP【附詳細(xì)案例】 | |
Spring事務(wù)簡介及相關(guān)案例 | |
Spring 事務(wù)管理方案和事務(wù)管理器及事務(wù)控制的API | |
Spring 事務(wù)的相關(guān)配置、傳播行為、隔離級別及注解配置聲明式事務(wù) |
到了這里,關(guān)于Spring IOC之對象的創(chuàng)建方式、策略及銷毀時(shí)機(jī)和生命周期且獲取方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!