![b站動(dòng)力節(jié)點(diǎn)Spring6框架學(xué)習(xí)筆記分享_第1頁(yè)](http://file4.renrendoc.com/view/6efb80c79d6d21e35a82f6f0c2ead90b/6efb80c79d6d21e35a82f6f0c2ead90b1.gif)
![b站動(dòng)力節(jié)點(diǎn)Spring6框架學(xué)習(xí)筆記分享_第2頁(yè)](http://file4.renrendoc.com/view/6efb80c79d6d21e35a82f6f0c2ead90b/6efb80c79d6d21e35a82f6f0c2ead90b2.gif)
![b站動(dòng)力節(jié)點(diǎn)Spring6框架學(xué)習(xí)筆記分享_第3頁(yè)](http://file4.renrendoc.com/view/6efb80c79d6d21e35a82f6f0c2ead90b/6efb80c79d6d21e35a82f6f0c2ead90b3.gif)
![b站動(dòng)力節(jié)點(diǎn)Spring6框架學(xué)習(xí)筆記分享_第4頁(yè)](http://file4.renrendoc.com/view/6efb80c79d6d21e35a82f6f0c2ead90b/6efb80c79d6d21e35a82f6f0c2ead90b4.gif)
![b站動(dòng)力節(jié)點(diǎn)Spring6框架學(xué)習(xí)筆記分享_第5頁(yè)](http://file4.renrendoc.com/view/6efb80c79d6d21e35a82f6f0c2ead90b/6efb80c79d6d21e35a82f6f0c2ead90b5.gif)
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Spring6第三章三、Spring的入門程序3.1Spring的下載官網(wǎng)地址:https://spring.io/官網(wǎng)地址(中文):/打開(kāi)Spring官網(wǎng)后,可以看到SpringFramework,以及通過(guò)SpringFramework衍生的其它框架:我們即將要學(xué)習(xí)的就是SpringFramework。怎么下載呢?第一步:進(jìn)入github第二步:找到下圖位置,點(diǎn)擊超鏈接第三步:找到下圖位置,點(diǎn)擊超鏈接第四步:按照下圖步驟操作第五步:繼續(xù)在springframework目錄下找下圖的spring,點(diǎn)開(kāi)之后你會(huì)看到很多不同的版本第六步:選擇對(duì)應(yīng)的版本第七步:點(diǎn)擊上圖的url點(diǎn)擊spring-5.3.9-dist.zip下載spring框架。將下載的zip包解壓:docs:spring框架的API幫助文檔libs:spring框架的jar文件(用spring框架就是用這些jar包)schema:spring框架的XML配置文件相關(guān)的約束文件3.2Spring的jar文件打開(kāi)libs目錄,會(huì)看到很多jar包:spring-core-5.3.9.jar:字節(jié)碼(這個(gè)是支撐程序運(yùn)行的jar包)spring-core-5.3.9-javadoc.jar:代碼中的注釋spring-core-5.3.9-sources.jar:源碼我們來(lái)看一下spring框架都有哪些jar包:JAR文件描述spring-aop-5.3.9.jar這個(gè)jar文件包含在應(yīng)用中使用Spring的AOP特性時(shí)所需的類spring-aspects-5.3.9.jar提供對(duì)AspectJ的支持,以便可以方便的將面向切面的功能集成進(jìn)IDE中spring-beans-5.3.9.jar這個(gè)jar文件是所有應(yīng)用都要用到的,它包含訪問(wèn)配置文件、創(chuàng)建和管理bean以及進(jìn)行InversionofControl/DependencyInjection(IoC/DI)操作相關(guān)的所有類。如果應(yīng)用只需基本的IoC/DI支持,引入spring-core.jar及spring-beans.jar文件就可以了。spring-context-5.3.9.jar這個(gè)jar文件為Spring核心提供了大量擴(kuò)展??梢哉业绞褂肧pringApplicationContext特性時(shí)所需的全部類,JDNI所需的全部類,instrumentation組件以及校驗(yàn)Validation方面的相關(guān)類。spring-context-indexer-5.3.9.jar雖然類路徑掃描非???,但是Spring內(nèi)部存在大量的類,添加此依賴,可以通過(guò)在編譯時(shí)創(chuàng)建候選對(duì)象的靜態(tài)列表來(lái)提高大型應(yīng)用程序的啟動(dòng)性能。spring-context-support-5.3.9.jar用來(lái)提供Spring上下文的一些擴(kuò)展模塊,例如實(shí)現(xiàn)郵件服務(wù)、視圖解析、緩存、定時(shí)任務(wù)調(diào)度等spring-core-5.3.9.jarSpring框架基本的核心工具類。Spring其它組件要都要使用到這個(gè)包里的類,是其它組件的基本核心,當(dāng)然你也可以在自己的應(yīng)用系統(tǒng)中使用這些工具類。spring-expression-5.3.9.jarSpring表達(dá)式語(yǔ)言。spring-instrument-5.3.9.jarSpring3.0對(duì)服務(wù)器的代理接口。spring-jcl-5.3.9.jarSpring的日志模塊。JCL,全稱為"JakartaCommonsLogging",也可稱為"ApacheCommonsLogging"。spring-jdbc-5.3.9.jarSpring對(duì)JDBC的支持。spring-jms-5.3.9.jar這個(gè)jar包提供了對(duì)JMS1.0.2/1.1的支持類。JMS是Java消息服務(wù)。屬于JavaEE規(guī)范之一。spring-messaging-5.3.9.jar為集成messagingapi和消息協(xié)議提供支持spring-orm-5.3.9.jarSpring集成ORM框架的支持,比如集成hibernate,mybatis等。spring-oxm-5.3.9.jar為主流O/XMapping組件提供了統(tǒng)一層抽象和封裝,OXM是ObjectXmlMapping。對(duì)象和XML之間的相互轉(zhuǎn)換。spring-r2dbc-5.3.9.jarReactiveRelationalDatabaseConnectivity(關(guān)系型數(shù)據(jù)庫(kù)的響應(yīng)式連接)的縮寫(xiě)。這個(gè)jar文件是Spring對(duì)r2dbc的支持。spring-test-5.3.9.jar對(duì)Junit等測(cè)試框架的簡(jiǎn)單封裝。spring-tx-5.3.9.jar為JDBC、Hibernate、JDO、JPA、Beans等提供的一致的聲明式和編程式事務(wù)管理支持。spring-web-5.3.9.jarSpring集成MVC框架的支持,比如集成Struts等。spring-webflux-5.3.9.jarWebFlux是Spring5添加的新模塊,用于web的開(kāi)發(fā),功能和SpringMVC類似的,Webflux使用當(dāng)前一種比較流程響應(yīng)式編程出現(xiàn)的框架。spring-webmvc-5.3.9.jarSpringMVC框架的類庫(kù)spring-websocket-5.3.9.jarSpring集成WebSocket框架時(shí)使用注意:如果你只是想用Spring的IoC功能,僅需要引入:spring-context即可。將這個(gè)jar包添加到classpath當(dāng)中。如果采用maven只需要引入context的依賴即可。<!--Spring6的正式版發(fā)布之前,這個(gè)倉(cāng)庫(kù)地址是需要的-->
<repositories>
<repository>
<id>estone</id>
<name>SpringMilestoneRepository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependencies>
<!--springcontext依賴:使用的是6.0.0-M2里程碑版-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
</dependencies>
3.3第一個(gè)Spring程序前期準(zhǔn)備:打開(kāi)IDEA創(chuàng)建EmptyProject:spring6設(shè)置JDK版本17,編譯器版本17設(shè)置IDEA的Maven:關(guān)聯(lián)自己的maven在空的工程spring6中創(chuàng)建第一個(gè)模塊:spring6-001-first第一步:添加springcontext的依賴,pom.xml配置如下<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.powernode</groupId>
<artifactId>spring6-001-first</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<repositories>
<repository>
<id>estone</id>
<name>SpringMilestoneRepository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependencies>
<!--springcontext依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
</dependencies>
<properties>
<piler.source>17</piler.source>
<piler.target>17</piler.target>
</properties>
</project>
注意:打包方式j(luò)ar。當(dāng)加入springcontext的依賴之后,會(huì)關(guān)聯(lián)引入其他依賴:springaop:面向切面編程springbeans:IoC核心springcore:spring的核心工具包springjcl:spring的日志包springexpression:spring表達(dá)式第二步:添加junit依賴<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.powernode</groupId>
<artifactId>spring6-001-first</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<repositories>
<repository>
<id>estone</id>
<name>SpringMilestoneRepository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependencies>
<!--springcontext依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<piler.source>17</piler.source>
<piler.target>17</piler.target>
</properties>
</project>
第三步:定義bean:Userpackagecom.powernode.spring6.bean;
/**
*bean,封裝用戶信息。
*@author動(dòng)力節(jié)點(diǎn)
*@version1.0
*@since1.0
*/
publicclassUser{
}
第四步:編寫(xiě)spring的配置文件:beans.xml。該文件放在類的根路徑下。上圖是使用IDEA工具自帶的spring配置文件的模板進(jìn)行創(chuàng)建。配置文件中進(jìn)行bean的配置。<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd">
<beanid="userBean"class="com.powernode.spring6.bean.User"/>
</beans>
bean的id和class屬性:id屬性:代表對(duì)象的唯一標(biāo)識(shí)。可以看做一個(gè)人的身份證號(hào)。class屬性:用來(lái)指定要?jiǎng)?chuàng)建的java對(duì)象的類名,這個(gè)類名必須是全限定類名(帶包名)。第五步:編寫(xiě)測(cè)試程序packagecom.powernode.spring6.test;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpring6Test{
@Test
publicvoidtestFirst(){
//初始化Spring容器上下文(解析beans.xml文件,創(chuàng)建所有的bean對(duì)象)
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("beans.xml");
//根據(jù)id獲取bean對(duì)象
ObjectuserBean=applicationContext.getBean("userBean");
System.out.println(userBean);
}
}
第七步:運(yùn)行測(cè)試程序3.4第一個(gè)Spring程序詳細(xì)剖析<beanid="userBean"class="com.powernode.spring6.bean.User"/>
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("beans.xml");
ObjectuserBean=applicationContext.getBean("userBean");
bean標(biāo)簽的id屬性可以重復(fù)嗎?packagecom.powernode.spring6.bean;
/**
*@author動(dòng)力節(jié)點(diǎn)
*@version1.0
*@classNameVip
*@since1.0
**/
publicclassVip{
}
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd">
<beanid="userBean"class="com.powernode.spring6.bean.User"/>
<beanid="userBean"class="com.powernode.spring6.bean.Vip"/>
</beans>
運(yùn)行測(cè)試程序:通過(guò)測(cè)試得出:在spring的配置文件中id是不能重名。底層是怎么創(chuàng)建對(duì)象的,是通過(guò)反射機(jī)制調(diào)用無(wú)參數(shù)構(gòu)造方法嗎?packagecom.powernode.spring6.bean;
/**
*bean,封裝用戶信息。
*@author動(dòng)力節(jié)點(diǎn)
*@version1.0
*@since1.0
*/
publicclassUser{
publicUser(){
System.out.println("User的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
}
}
在User類中添加無(wú)參數(shù)構(gòu)造方法,如上。運(yùn)行測(cè)試程序:通過(guò)測(cè)試得知:創(chuàng)建對(duì)象時(shí)確實(shí)調(diào)用了無(wú)參數(shù)構(gòu)造方法。如果提供一個(gè)有參數(shù)構(gòu)造方法,不提供無(wú)參數(shù)構(gòu)造方法會(huì)怎樣呢?packagecom.powernode.spring6.bean;
/**
*bean,封裝用戶信息。
*@author動(dòng)力節(jié)點(diǎn)
*@version1.0
*@since1.0
*/
publicclassUser{
/*publicUser(){
System.out.println("User的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
}*/
publicUser(Stringname){
System.out.println("User的有參數(shù)構(gòu)造方法執(zhí)行");
}
}
運(yùn)行測(cè)試程序:通過(guò)測(cè)試得知:spring是通過(guò)調(diào)用類的無(wú)參數(shù)構(gòu)造方法來(lái)創(chuàng)建對(duì)象的,所以要想讓spring給你創(chuàng)建對(duì)象,必須保證無(wú)參數(shù)構(gòu)造方法是存在的。Spring是如何創(chuàng)建對(duì)象的呢?原理是什么?//dom4j解析beans.xml文件,從中獲取class的全限定類名
//通過(guò)反射機(jī)制調(diào)用無(wú)參數(shù)構(gòu)造方法創(chuàng)建對(duì)象
Classclazz=Class.forName("com.powernode.spring6.bean.User");
Objectobj=clazz.newInstance();
把創(chuàng)建好的對(duì)象存儲(chǔ)到一個(gè)什么樣的數(shù)據(jù)結(jié)構(gòu)當(dāng)中了呢?spring配置文件的名字必須叫做beans.xml嗎?ApplicationContextapplicationContext=newClassPathXmlApplicationContext("beans.xml");
通過(guò)以上的java代碼可以看出,這個(gè)spring配置文件名字是我們負(fù)責(zé)提供的,顯然spring配置文件的名字是隨意的。像這樣的beans.xml文件可以有多個(gè)嗎?再創(chuàng)建一個(gè)spring配置文件,起名:spring.xml<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd">
<beanid="vipBean"class="com.powernode.spring6.bean.Vip"/>
</beans>
packagecom.powernode.spring6.test;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpring6Test{
@Test
publicvoidtestFirst(){
//初始化Spring容器上下文(解析beans.xml文件,創(chuàng)建所有的bean對(duì)象)
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("beans.xml","spring.xml");
//根據(jù)id獲取bean對(duì)象
ObjectuserBean=applicationContext.getBean("userBean");
ObjectvipBean=applicationContext.getBean("vipBean");
System.out.println(userBean);
System.out.println(vipBean);
}
}
運(yùn)行測(cè)試程序:通過(guò)測(cè)試得知,spring的配置文件可以有多個(gè),在ClassPathXmlApplicationContext構(gòu)造方法的參數(shù)上傳遞文件路徑即可。這是為什么呢?通過(guò)源碼可以看到:在配置文件中配置的類必須是自定義的嗎,可以使用JDK中的類嗎,例如:java.util.Date?<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd">
<beanid="userBean"class="com.powernode.spring6.bean.User"/>
<!--<beanid="userBean"class="com.powernode.spring6.bean.Vip"/>-->
<beanid="dateBean"class="java.util.Date"/>
</beans>
packagecom.powernode.spring6.test;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpring6Test{
@Test
publicvoidtestFirst(){
//初始化Spring容器上下文(解析beans.xml文件,創(chuàng)建所有的bean對(duì)象)
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("beans.xml","spring.xml");
//根據(jù)id獲取bean對(duì)象
ObjectuserBean=applicationContext.getBean("userBean");
ObjectvipBean=applicationContext.getBean("vipBean");
ObjectdateBean=applicationContext.getBean("dateBean");
System.out.println(userBean);
System.out.println(vipBean);
System.out.println(dateBean);
}
}
運(yùn)行測(cè)試程序:通過(guò)測(cè)試得知,在spring配置文件中配置的bean可以任意類,只要這個(gè)類不是抽象的,并且提供了無(wú)參數(shù)構(gòu)造方法。getBean()方法調(diào)用時(shí),如果指定的id不存在會(huì)怎樣?運(yùn)行測(cè)試程序:通過(guò)測(cè)試得知,當(dāng)id不存在的時(shí)候,會(huì)出現(xiàn)異常。getBean()方法返回的類型是Object,如果訪問(wèn)子類的特有屬性和方法時(shí),還需要向下轉(zhuǎn)型,有其它辦法可以解決這個(gè)問(wèn)題嗎?Useruser=applicationContext.getBean("userBean",User.class);
ClassPathXmlApplicationContext是從類路徑中加載配置文件,如果沒(méi)有在類路徑當(dāng)中,又應(yīng)該如何加載配置文件呢?<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd">
<beanid="vipBean2"class="com.powernode.spring6.bean.Vip"/>
</beans>
ApplicationContextapplicationContext2=newFileSystemXmlApplicationContext("d:/spring6.xml");
Vipvip=applicationContext2.getBean("vipBean2",Vip.class);
System.out.println(vip);
沒(méi)有在類路徑中的話,需要使用FileSystemXmlApplicationContext類進(jìn)行加載配置文件。這種方式較少用。一般都是將配置文件放到類路徑當(dāng)中,這樣可移植性更強(qiáng)。ApplicationContext的超級(jí)父接口BeanFactory。BeanFactorybeanFactory=newClassPathXmlApplicationContext("spr
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030全球桌面排版系統(tǒng)行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025-2030全球醫(yī)療設(shè)備安全解決方案行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025年全球及中國(guó)一次性甲狀腺穿刺器行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2025-2030全球亞歷山大變石激光器行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025廣州市農(nóng)村集體經(jīng)濟(jì)承包合同管理規(guī)定
- 勞務(wù)派遣合同協(xié)議模板范本
- 2025地區(qū)展柜、物料定作布展合同
- 個(gè)人連帶擔(dān)保合同
- 房屋場(chǎng)地租賃合同
- 砌筑勞務(wù)分包合同范本
- 《中國(guó)古代寓言》導(dǎo)讀(課件)2023-2024學(xué)年統(tǒng)編版語(yǔ)文三年級(jí)下冊(cè)
- 五年級(jí)上冊(cè)計(jì)算題大全1000題帶答案
- 工程建設(shè)行業(yè)標(biāo)準(zhǔn)內(nèi)置保溫現(xiàn)澆混凝土復(fù)合剪力墻技術(shù)規(guī)程
- 液壓動(dòng)力元件-柱塞泵課件講解
- 人教版五年級(jí)上冊(cè)數(shù)學(xué)脫式計(jì)算100題及答案
- 屋面細(xì)石混凝土保護(hù)層施工方案及方法
- 2024年1月山西省高三年級(jí)適應(yīng)性調(diào)研測(cè)試(一模)理科綜合試卷(含答案)
- 110kv各類型變壓器的計(jì)算單
- 5A+Chapter+1+Changes+at+home+課件(新思維小學(xué)英語(yǔ))
- 安徽省2023年中考數(shù)學(xué)試卷(附答案)
- 護(hù)工(陪護(hù))培訓(xùn)教材(完整版)資料
評(píng)論
0/150
提交評(píng)論