国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC

這篇具有很好參考價(jià)值的文章主要介紹了Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

學(xué)習(xí)目標(biāo)

上一篇文章我們介紹了什么是Spring,以及Spring的一些核心概念,并且快速快發(fā)一個(gè)Spring項(xiàng)目,實(shí)現(xiàn)IOC和DI,今天具體來講解IOC

能夠說出IOC的基礎(chǔ)配置和Bean作用域

了解Bean的生命周期

能夠說出Bean的實(shí)例化方式

一、Bean的基礎(chǔ)配置

問題導(dǎo)入

問題1:在<bean>標(biāo)簽上如何配置別名?

問題2:Bean的默認(rèn)作用范圍是什么?如何修改?

1 Bean基礎(chǔ)配置【重點(diǎn)】

配置說明

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

2 Bean別名配置

配置說明

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

注意事項(xiàng):

獲取bean無論是通過id還是name獲取,如果無法獲取到,將拋出異常NoSuchBeanDefinitionException
NoSuchBeanDefinitionException:?No?bean?named?'studentDaoImpl'?available
代碼演示

【第0步】創(chuàng)建項(xiàng)目名稱為10_2_IOC_Bean的maven項(xiàng)目

【第一步】導(dǎo)入Spring坐標(biāo)

??<dependencies>
??????<!--導(dǎo)入spring的坐標(biāo)spring-context,對應(yīng)版本是5.2.10.RELEASE-->
??????<dependency>
??????????<groupId>org.springframework</groupId>
??????????<artifactId>spring-context</artifactId>
??????????<version>5.3.15</version>
??????</dependency>

??????<!--?導(dǎo)入junit的測試包?-->
??????<dependency>
??????????<groupId>org.junit.jupiter</groupId>
??????????<artifactId>junit-jupiter</artifactId>
??????????<version>5.8.2</version>
??????????<scope>test</scope>
??????</dependency>

??????<dependency>
??????????<groupId>org.projectlombok</groupId>
??????????<artifactId>lombok</artifactId>
??????????<version>1.18.28</version>
??????</dependency>
??</dependencies>

【第二步】導(dǎo)入Student實(shí)體類

@Data
@ToString
@AllArgsConstructor
public?class?Student?{
????private?String?name;
????private?String?address;
????private?Integer?age;

????private?Integer?status;
}

【第三步】定義Spring管理的類(接口)

  • StudentDao接口和StudentDaoImpl實(shí)現(xiàn)類

package?com.zbbmeta.dao;

public?interface?StudentDao?{
????/**
?????*?添加學(xué)生
?????*/
????void?save();
}
package?com.zbbmeta.dao.impl;

import?com.zbbmeta.dao.StudentDao;

public?class?StudentDaoImpl?implements?StudentDao?{
????@Override
????public?void?save()?{
????????System.out.println("DAO:?添加學(xué)生信息到數(shù)據(jù)庫...");
????}
}

  • StudentService接口和StudentServiceImpl實(shí)現(xiàn)類

package?com.zbbmeta.service;

public?interface?StudentService?{
????/**
?????*?添加學(xué)生
?????*/
????void?save();
}

package?com.zbbmeta.service.impl;

import?com.zbbmeta.dao.StudentDao;
import?com.zbbmeta.dao.impl.StudentDaoImpl;
import?com.zbbmeta.service.StudentService;

public?class?StudentServiceImpl?implements?StudentService?{

????//創(chuàng)建成員對象
????private?StudentDao?studentDao?=?new?StudentDaoImpl();
????@Override
????public?void?save()?{

????}
}

【第四步】創(chuàng)建Spring配置文件在resources目錄下,配置對應(yīng)類作為Spring管理的bean對象

  • 定義application.xml配置文件并配置StudentDaoImpl

<?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">
????<!--
????目標(biāo):熟悉bean標(biāo)簽詳細(xì)的屬性應(yīng)用
????-->
????<!--
????name屬性:可以設(shè)置多個(gè)別名,別名之間使用逗號,空格,分號等分隔
????-->
????<bean?name="studentDao2,abc?studentDao3"?class="com.zbbmeta.dao.impl.StudentDaoImpl"?id="studentDao"></bean>

</beans>

注意事項(xiàng):bean定義時(shí)id屬性和name中名稱不能有重復(fù)的在同一個(gè)上下文中(IOC容器中)不能重復(fù)

【第四步】根據(jù)容器別名獲取Bean對象

package?com.zbbmeta;

import?com.zbbmeta.dao.StudentDao;
import?org.springframework.context.support.ClassPathXmlApplicationContext;

public?class?NameApplication?{
????public?static?void?main(String[]?args)?{
????????/**
?????????*?從IOC容器里面根據(jù)別名獲取對象執(zhí)行
?????????*/
????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????//2.從IOC容器里面獲取id="abc"對象
????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("abc");
????????//3.執(zhí)行對象方法
????????studentDao.save();
????????//4.關(guān)閉容器
????????ac.close();
????}
}
打印結(jié)果

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

3 Bean作用范圍配置【重點(diǎn)】

配置說明

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

擴(kuò)展: scope的取值不僅僅只有singleton和prototype,還有request、session、application、 websocket ,表示創(chuàng)建出的對象放置在web容器(tomcat)對應(yīng)的位置。比如:request表示保存到request域中。

代碼演示

在application.xml中配置prototype格式

  • 定義application.xml配置文件并配置StudentDaoImpl

<?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">
????<!--
????目標(biāo):熟悉bean標(biāo)簽詳細(xì)的屬性應(yīng)用
????-->
????<!--
????scope屬性:定義bean的作用范圍,一共有5個(gè)
?????????singleton:?設(shè)置單例創(chuàng)建對象(推薦,也是默認(rèn)值),好處:節(jié)省資源
?????????prototype:?設(shè)置多例創(chuàng)建對象,每次從IOC容器獲取的時(shí)候都會創(chuàng)建對象,獲取多次創(chuàng)建多次。
?????????request:?在web開發(fā)環(huán)境中,IOC容器會將對象放到request請求域中,對象存活的范圍在一次請求內(nèi)。
??????????????????請求開始創(chuàng)建對象,請求結(jié)束銷毀對象
?????????session:?在web開發(fā)環(huán)境中,IOC容器會將對象放到session會話域中,對象存活的范圍在一次會話內(nèi)。
??????????????????會話開始開始創(chuàng)建對象,會話銷毀對象銷毀。
?????????global-session:?是多臺服務(wù)器共享同一個(gè)會話存儲的數(shù)據(jù)。
????-->
????<bean?class="com.zbbmeta.dao.impl.StudentDaoImpl"?id="studentDao4"?scope="prototype"></bean>

</beans>

根據(jù)容器別名獲取Bean對象

package?com.zbbmeta;

import?com.zbbmeta.dao.StudentDao;
import?org.springframework.context.support.ClassPathXmlApplicationContext;

public?class?ScopeApplication?{
????public?static?void?main(String[]?args)?{
????????/**
?????????*?Bean的作用域范圍演示
?????????*/
????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????//2.從IOC容器里面獲取id="studentService"對象
????????System.out.println("=========singleton(單例)模式=========");
????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("studentDao");
????????StudentDao?studentDao1?=?(StudentDao)?ac.getBean("studentDao");
????????System.out.println("studentDao?=?"?+?studentDao);
????????System.out.println("studentDao1?=?"?+?studentDao1);
????????System.out.println("=========prototype模式=========");
????????StudentDao?studentDao2?=?(StudentDao)?ac.getBean("studentDao4");
????????StudentDao?studentDao3?=?(StudentDao)?ac.getBean("studentDao4");
????????System.out.println("studentDao2?=?"?+?studentDao2);
????????System.out.println("studentDao3?=?"?+?studentDao3);
????????//4.關(guān)閉容器
????????ac.close();
????}
}

打印結(jié)果

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

注意:在我們的實(shí)際開發(fā)當(dāng)中,絕大部分的Bean是單例的,也就是說絕大部分Bean不需要配置scope屬性。

二、Bean的實(shí)例化

思考:Bean的實(shí)例化方式有幾種?

2 實(shí)例化Bean的三種方式

2.1 構(gòu)造方法方式【重點(diǎn)】
  • BookDaoImpl實(shí)現(xiàn)類

public?class?StudentDaoImpl?implements?StudentDao?{
????public?StudentDaoImpl()?{
????????System.out.println("Student?dao?constructor?is?running?....");
????}

????@Override
????public?void?save()?{
????????System.out.println("DAO:?添加學(xué)生信息到數(shù)據(jù)庫...");
????}
}
  • application.xml配置

????<!--
????目標(biāo):講解bean創(chuàng)建方式
????-->
????<!--創(chuàng)建StudentDaoImpl對象方式1:默認(rèn)調(diào)用類的無參構(gòu)造函數(shù)創(chuàng)建-->
????<bean?id="studentDao"?class="com.zbbmeta.dao.impl.StudentDaoImpl"/>
  • AppForInstanceBook測試類

public?class?OneApplication?{
????public?static?void?main(String[]?args)?{
????????/**
?????????*?無參構(gòu)造方式創(chuàng)建Bean
?????????*/
????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????//2.從IOC容器里面獲取id="studentService"對象
????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("studentDao");
????????//3.執(zhí)行對象方法
????????studentDao.save();
????????//4.關(guān)閉容器
????????ac.close();
????}
}
  • 運(yùn)行結(jié)果

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

注意:無參構(gòu)造方法如果不存在,將拋出異常BeanCreationException

2.2 靜態(tài)工廠方式
  • StudentDaoFactory工廠類

package?com.zbbmeta.factory;

import?com.zbbmeta.dao.StudentDao;
import?com.zbbmeta.dao.impl.StudentDaoImpl;

public?class?StudentDaoFactory?{
//????靜態(tài)工廠創(chuàng)建對象
????public?static?StudentDao?getStudentDao(){
????????System.out.println("Student?static?factory?setup....");
????????return?new?StudentDaoImpl();
????}
}
  • applicationContext.xml配置

<?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">

????<!--
????目標(biāo):講解bean創(chuàng)建方式
????-->
????<!--創(chuàng)建StudentDaoImpl對象方式1:默認(rèn)調(diào)用類的無參構(gòu)造函數(shù)創(chuàng)建-->
<!--????<bean?id="studentDao"?class="com.zbbmeta.dao.impl.StudentDaoImpl"/>-->

????<!--創(chuàng)建StudentDaoImpl對象方式2:調(diào)用靜態(tài)工廠方法創(chuàng)建對象加入IOC容器
????class="com.zbbmeta.factory.StudentDaoFactory"?設(shè)置工廠類全名
????factory-method="getStudentDao"?調(diào)用工廠的靜態(tài)方法
-->
????<bean?class="com.zbbmeta.factory.StudentDaoFactory"?factory-method="getStudentDao"?id="studentDao2"></bean>


</beans>

注意:測試前最好把之前使用Bean標(biāo)簽創(chuàng)建的對象進(jìn)行注釋

  • TwoApplication測試類

public?class?TwoApplication?{
????public?static?void?main(String[]?args)?{
????????/**
?????????*?無參構(gòu)造方式創(chuàng)建Bean
?????????*/
????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????//2.從IOC容器里面獲取id="studentService"對象
????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("studentDao2");
????????//3.執(zhí)行對象方法
????????studentDao.save();
????????//4.關(guān)閉容器
????????ac.close();
????}
}
  • 運(yùn)行結(jié)果

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

2.3 實(shí)例工廠方式
  • UserDao接口和UserDaoImpl實(shí)現(xiàn)類

????//利用實(shí)例方法創(chuàng)建StudentDao對象
????public?StudentDao?getStudentDao2(){
????????System.out.println("調(diào)用了實(shí)例工廠方法");
????????return?new?StudentDaoImpl();
????}
  • StudentDaoFactory工廠類添加方法

//實(shí)例工廠創(chuàng)建對象
public?class?UserDaoFactory?{
????public?UserDao?getUserDao(){
????????return?new?UserDaoImpl();
????}
}
  • applicationContext.xml配置

<?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">

????<!--
????目標(biāo):講解bean創(chuàng)建方式
????-->
????<!--創(chuàng)建StudentDaoImpl對象方式1:默認(rèn)調(diào)用類的無參構(gòu)造函數(shù)創(chuàng)建-->
<!--????<bean?id="studentDao"?class="com.zbbmeta.dao.impl.StudentDaoImpl"/>-->

????<!--創(chuàng)建StudentDaoImpl對象方式2:調(diào)用靜態(tài)工廠方法創(chuàng)建對象加入IOC容器
????class="com.zbbmeta.factory.StudentDaoFactory"?設(shè)置工廠類全名
????factory-method="getStudentDao"?調(diào)用工廠的靜態(tài)方法
-->
<!--????<bean?class="com.zbbmeta.factory.StudentDaoFactory"?factory-method="getStudentDao"?id="studentDao2"></bean>-->


????<!--創(chuàng)建BookDaoImpl對象方式3:調(diào)用實(shí)例工廠方法創(chuàng)建對象加入IOC容器
????class="com.itheima.factory.BookDaoFactory"?設(shè)置工廠類全名
????factory-method="getBookDao"?調(diào)用工廠的靜態(tài)方法
-->
????<!--第一步:創(chuàng)建工廠StudentDaoFactory對象-->
????<bean?class="com.zbbmeta.factory.StudentDaoFactory"?id="studentDaoFactory"></bean>
????<!--第一步:調(diào)用工廠對象的getStudentDao2()實(shí)例方法創(chuàng)建StudentDaoImpl對象加入IOC容器
????????factory-bean="studentDaoFactory"?獲取IOC容器中指定id值的對象
????????factory-method="getStudentDao2"?如果配置了factory-bean,那么這里設(shè)置的就是實(shí)例方法名
????-->
????<bean?factory-bean="studentDaoFactory"?factory-method="getStudentDao2"?id="studentDao3"></bean>


</beans>
  • ThreeApplication測試類

package?com.zbbmeta;

import?com.zbbmeta.dao.StudentDao;
import?org.springframework.context.support.ClassPathXmlApplicationContext;

public?class?ThreeApplication?{
????public?static?void?main(String[]?args)?{
????????/**
?????????*?無參構(gòu)造方式創(chuàng)建Bean
?????????*/
????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????//2.從IOC容器里面獲取id="studentService"對象
????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("studentDao3");
????????//3.執(zhí)行對象方法
????????studentDao.save();
????????//4.關(guān)閉容器
????????ac.close();
????}
}

  • 運(yùn)行結(jié)果

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

三、Bean的生命周期【了解】

問題導(dǎo)入

問題1:多例的Bean能夠配置并執(zhí)行銷毀的方法?

問題2:如何做才執(zhí)行Bean銷毀的方法?

1 生命周期相關(guān)概念介紹

  • 生命周期:從創(chuàng)建到消亡的完整過程

  • bean生命周期:bean從創(chuàng)建到銷毀的整體過程

  • bean生命周期控制:在bean創(chuàng)建后到銷毀前做一些事情

1.1生命周期過程
  • 初始化容器
    • 創(chuàng)建對象(內(nèi)存分配)

    • 執(zhí)行構(gòu)造方法

    • 執(zhí)行屬性注入(set操作)

    • 執(zhí)行bean初始化方法

  • 使用bean
    • 執(zhí)行業(yè)務(wù)操作

  • 關(guān)閉/銷毀容器
    • 執(zhí)行bean銷毀方法

2 代碼演示

2.1 Bean生命周期控制

【第0步】創(chuàng)建項(xiàng)目名稱為10_4_IOC_BeanLifeCycle的maven項(xiàng)目

【第一步】導(dǎo)入Spring坐標(biāo)

??<dependencies>
??????<!--導(dǎo)入spring的坐標(biāo)spring-context,對應(yīng)版本是5.2.10.RELEASE-->
??????<dependency>
??????????<groupId>org.springframework</groupId>
??????????<artifactId>spring-context</artifactId>
??????????<version>5.3.15</version>
??????</dependency>

??????<!--?導(dǎo)入junit的測試包?-->
??????<dependency>
??????????<groupId>org.junit.jupiter</groupId>
??????????<artifactId>junit-jupiter</artifactId>
??????????<version>5.8.2</version>
??????????<scope>test</scope>
??????</dependency>

??????<dependency>
??????????<groupId>org.projectlombok</groupId>
??????????<artifactId>lombok</artifactId>
??????????<version>1.18.28</version>
??????</dependency>
??</dependencies>

【第二步】導(dǎo)入Student實(shí)體類

@Data
@ToString
@AllArgsConstructor
public?class?Student?{
????private?String?name;
????private?String?address;
????private?Integer?age;

????private?Integer?status;
}

【第三步】定義Spring管理的類(接口)

  • StudentDao接口和StudentDaoImpl實(shí)現(xiàn)類

package?com.zbbmeta.dao;

public?interface?StudentDao?{
????/**
?????*?添加學(xué)生
?????*/
????void?save();
}
package?com.zbbmeta.dao.impl;

import?com.zbbmeta.dao.StudentDao;

public?class?StudentDaoImpl?implements?StudentDao?{

????public?StudentDaoImpl(){
????????System.out.println("Student?Dao?的無參構(gòu)造");
????}

????@Override
????public?void?save()?{
????????System.out.println("DAO:?添加學(xué)生信息到數(shù)據(jù)庫...");
????}

????public?void?init(){
????????System.out.println("Student?Dao?的初始化方法");
????}

????public?void?destroy(){
????????System.out.println("Student?Dao?的銷毀方法");
????}
}

【第四步】創(chuàng)建Spring配置文件在resources目錄下,配置對應(yīng)類作為Spring管理的bean對象

  • 定義application.xml配置文件并配置StudentDaoImpl

<?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">
????<!--目標(biāo):創(chuàng)建StudentDaoImpl對象:設(shè)置生命周期方法
????????init-method="init"?在對象創(chuàng)建后立即調(diào)用初始化方法
????????destroy-method="destroy":在容器執(zhí)行銷毀前立即調(diào)用銷毀的方法
????????注意:只有單例對象才會運(yùn)行銷毀生命周期方法
-->
????<bean?class="com.zbbmeta.dao.impl.StudentDaoImpl"?id="studentDao"?init-method="init"?destroy-method="destroy"></bean>
</beans>

【第四步】根據(jù)容器別名獲取Bean對象

package?com.zbbmeta;

import?com.zbbmeta.dao.StudentDao;
import?org.springframework.context.support.ClassPathXmlApplicationContext;

public?class?LifeCycleApplication?{
????public?static?void?main(String[]?args)?{


????????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????????//2.從IOC容器里面獲取id="studentService"對象
????????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("studentDao");
????????????//3.執(zhí)行對象方法
????????????studentDao.save();
????????????//4.關(guān)閉容器
????????????ac.close();
????????
????}
}

打印結(jié)果

Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC,JavaWeb,spring,xml,數(shù)據(jù)庫

3 Bean銷毀時(shí)機(jī)

  • 容器關(guān)閉前觸發(fā)bean的銷毀

  • 關(guān)閉容器方式:
    • 手工關(guān)閉容器 調(diào)用容器的close()操作

    • 注冊關(guān)閉鉤子(類似于注冊一個(gè)事件),在虛擬機(jī)退出前先關(guān)閉容器再退出虛擬機(jī) 調(diào)用容器的registerShutdownHook()操作文章來源地址http://www.zghlxwxcb.cn/news/detail-635980.html

public?class?LifeCycleApplication?{
????public?static?void?main(String[]?args)?{


????????????//1.根據(jù)配置文件application.xml創(chuàng)建IOC容器
????????????ClassPathXmlApplicationContext?ac?=?new?ClassPathXmlApplicationContext("application.xml");
????????????//2.從IOC容器里面獲取id="studentService"對象
????????????StudentDao?studentDao?=?(StudentDao)?ac.getBean("studentDao");
????????????//3.執(zhí)行對象方法
????????????studentDao.save();
????????????//4.關(guān)閉容器
//????????????ac.close();
????????//注冊關(guān)閉鉤子函數(shù),在虛擬機(jī)退出之前回調(diào)此函數(shù),關(guān)閉容器
????????ac.registerShutdownHook();
????}
}

到了這里,關(guān)于Spring-1-透徹理解Spring XML的Bean創(chuàng)建--IOC的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 4.是人就能學(xué)會的Spring源碼教程-IOC容器創(chuàng)建Bean對象

    4.是人就能學(xué)會的Spring源碼教程-IOC容器創(chuàng)建Bean對象

    我們要關(guān)注一個(gè)接口 BeanFactory ,它是Spring IOC容器的根接口,也是容器的入口。 類的描述中已經(jīng)清楚的說明了: 我們來看一下這個(gè)接口里面的方法。 我們可以看到有各種各樣的 getBean 方法,讓我們可以從容器中獲取到各種各樣的Bean對象。 BeanFactory 有一個(gè)實(shí)現(xiàn)類 DefaultListab

    2024年02月05日
    瀏覽(25)
  • Spring IOC - Bean的掃描

    Spring IOC - Bean的掃描

    ????????@Component及其衍生注解:@Configuration、@Controller、@Service、@Repository標(biāo)記的類,被Spring IOC掃描到后,即可被容器管理起來。其原理基本涵蓋在AnnotationConfigApplicationContext構(gòu)造函數(shù)體的三行代碼里。 ????????這個(gè)無參構(gòu)造方法做了三件事情: 首先創(chuàng)建bean工廠,實(shí)際上

    2024年02月16日
    瀏覽(17)
  • 【Spring篇】IOC/DI配置管理第三方bean

    【Spring篇】IOC/DI配置管理第三方bean

    ??系列專欄:Spring系列專欄 ??個(gè)人主頁:個(gè)人主頁 目錄 一、案例:數(shù)據(jù)源對象管理 1.環(huán)境準(zhǔn)備 2.實(shí)現(xiàn)Druid管理 3.實(shí)現(xiàn)C3P0管理 二、加載properties文件 1.第三方bean屬性優(yōu)化 2.讀取單個(gè)屬性 3.注意事項(xiàng) ? 三、核心容器 1.環(huán)境準(zhǔn)備 2.容器 ? 1.容器的創(chuàng)建方式 2.Bean的三種獲取方式 3.容器

    2024年02月02日
    瀏覽(25)
  • 【Spring專題】Spring之Bean的生命周期源碼解析——階段二(IOC之實(shí)例化)

    【Spring專題】Spring之Bean的生命周期源碼解析——階段二(IOC之實(shí)例化)

    由于Spring源碼分析是一個(gè)前后聯(lián)系比較強(qiáng)的過程,而且這邊分析,也是按照代碼順序講解的,所以不了解前置知識的情況下,大概率沒辦法看懂當(dāng)前的內(nèi)容。所以,特別推薦看看我前面的文章(自上而下次序): Spring底層核心原理解析——引導(dǎo)篇【學(xué)習(xí)難度: ★★☆☆☆ 】

    2024年02月13日
    瀏覽(38)
  • 【仿寫spring之ioc篇】四、實(shí)現(xiàn)bean的初始化階段

    【仿寫spring之ioc篇】四、實(shí)現(xiàn)bean的初始化階段

    在Bean的初始化階段有前置和后置方法,這個(gè)方法是通過BeanPostProcessor來管理的,下面我們對原有的項(xiàng)目結(jié)構(gòu)做小小的更改。 對啟動類作出修改,先檢查有沒有BeanPostProcessor的實(shí)現(xiàn)類,有的話就使用,沒有就使用默認(rèn)的。 第二次循環(huán)先檢查是不是postProcessor,是的話就跳過就行

    2024年02月10日
    瀏覽(22)
  • 11Spring IoC注解式開發(fā)(上)(元注解/聲明Bean的注解/注解的使用/負(fù)責(zé)實(shí)例化Bean的注解)

    11Spring IoC注解式開發(fā)(上)(元注解/聲明Bean的注解/注解的使用/負(fù)責(zé)實(shí)例化Bean的注解)

    注解的存在主要是為了簡化XML的配置。Spring6倡導(dǎo)全注解開發(fā)。 注解開發(fā)的優(yōu)點(diǎn) :提高開發(fā)效率 注解開發(fā)的缺點(diǎn) :在一定程度上違背了OCP原則,使用注解的開發(fā)的前提是需求比較固定,變動較小。 自定義一個(gè)注解: 該注解上面修飾的注解包括:Target注解和Retention注解,這兩個(gè)注

    2024年01月21日
    瀏覽(30)
  • 【Spring專題】Spring之Bean的生命周期源碼解析——階段二(二)(IOC之屬性填充/依賴注入)

    【Spring專題】Spring之Bean的生命周期源碼解析——階段二(二)(IOC之屬性填充/依賴注入)

    由于Spring源碼分析是一個(gè)前后聯(lián)系比較強(qiáng)的過程,而且這邊分析,也是按照代碼順序講解的,所以不了解前置知識的情況下,大概率沒辦法看懂當(dāng)前的內(nèi)容。所以,特別推薦看看我前面的文章(自上而下次序): Spring底層核心原理解析【學(xué)習(xí)難度: ★★☆☆☆ 】 手寫簡易

    2024年02月12日
    瀏覽(25)
  • 【Spring教程11】Spring框架實(shí)戰(zhàn):IOC/DI注解開發(fā)管理第三方bean的全面深入詳解

    【Spring教程11】Spring框架實(shí)戰(zhàn):IOC/DI注解開發(fā)管理第三方bean的全面深入詳解

    歡迎大家回到《 Java教程之Spring30天快速入門》,本教程所有示例均基于Maven實(shí)現(xiàn),如果您對Maven還很陌生,請移步本人的博文《 如何在windows11下安裝Maven并配置以及 IDEA配置Maven環(huán)境》,本文的上一篇為《 純注解開發(fā)模式下的依賴注入和讀取properties配置文件》 前面定義bean的時(shí)

    2024年02月04日
    瀏覽(29)
  • 從入門到精通:掌握Spring IOC/DI配置管理第三方bean的技巧

    從入門到精通:掌握Spring IOC/DI配置管理第三方bean的技巧

    以后我們會用到很多第三方的bean,我們以數(shù)據(jù)源是 Druid(德魯伊) 和 C3P0 來配置舉個(gè)例子。 1.1.1 環(huán)境準(zhǔn)備 先來準(zhǔn)備下案例環(huán)境: 1.1.2 思路分析 需求:使用Spring的IOC容器來管理Druid連接池對象 1.使用第三方的技術(shù),需要在pom.xml添加依賴 2.在配置文件中將【第三方的類】制作成一個(gè)

    2024年02月02日
    瀏覽(24)
  • Spring系列二:基于XML配置bean

    Spring系列二:基于XML配置bean

    上文中, 我們學(xué)習(xí)到了 Spring系列一:spring的安裝與使用 接下來我們學(xué)習(xí), 通過XML配置bean Bean管理包括兩方面: 創(chuàng)建bean對象, 給bean注入屬性 案例: 通過spring的ioc容器, 獲取一個(gè)bean對象, 獲取方式: 按類型. 演示通過bean的類型獲取對象 細(xì)節(jié) 按照類型獲取bean, 要求ioc容器中的同一個(gè)

    2024年02月14日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包