日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring初识

發(fā)布時(shí)間:2023/12/9 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring初识 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

從上學(xué)期開始決心開始學(xué)習(xí)Spring,自己總是利用不好時(shí)間,到處瞎忙,結(jié)果浪費(fèi)了好多時(shí)間。想著利用暑假的時(shí)間,專心看會(huì)兒書。最初我在Spring官網(wǎng)下載jar包的時(shí)候,忙會(huì)兒了半天愣是沒找到下載的鏈接,瞬間覺得學(xué)Spring好難,莫名的抵觸心理開始泛濫,無奈只好強(qiáng)抱度娘。這是我找到的鏈接。

教您怎么從spring 官網(wǎng)下載參考文檔

附上小圖:

Spring官網(wǎng)下載

嘿嘿,其實(shí)好簡(jiǎn)單哦,可是人家當(dāng)時(shí)就是沒找到嘛,也不想那么快地?fù)肀Ф饶?3333……

Spring下載解壓后可以看到lib目錄下jar包確實(shí)有點(diǎn)多哦,每個(gè)jar包都有三個(gè)不同類型的,一種是含.class文件的jar包(….jar),這也是我們項(xiàng)目中需要的,還有兩種是幫助文檔(…-javadoc.jar)和源碼(…-sources.jar)。以下內(nèi)容純粹是我的看書筆記哦,我是看的李剛的那本輕量級(jí)javaee。自己總是太快忘記一些事,還有一些人,所以記錄下羅。

我看書寫第一個(gè)Demo(setter、getter都刪掉羅):

src/cn/edu/cqupt/MySpring/bean/Person.java package cn.edu.cqupt.MySpring.bean.impl; public class Person { private String name;private int age;public String toString(){return "Name:"+this.name+",age:"+this.age;} } src/beans.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"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <bean id="person" class="cn.edu.cqupt.MySpring.bean.impl.Person"><property name="name" value="xiaofeig"/><property name="age" value="20"/> </bean> </beans> src/cn/edu/cqupt/MyString/test/TestMain.java package cn.edu.cqupt.MySpring.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.cqupt.MySpring.bean.impl.Person; public class TestMain {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");Person person=context.getBean("person",Person.class);System.out.println(person);} 打印結(jié)果:Name:xiaofeig,age:20

Spring的核心機(jī)制:依賴注入(Dependency Injection)

我們?cè)趚ml文件中配置了一個(gè)編號(hào)為person的實(shí)例之后,Spring容器就負(fù)責(zé)幫助我們來創(chuàng)建這個(gè)實(shí)例,這個(gè)實(shí)例包含兩個(gè)屬性,分別為name和age,通過property標(biāo)簽來設(shè)置它的值value。配置完成之后,Spring容器就通過反射機(jī)制調(diào)用Person類的無參構(gòu)造方法創(chuàng)建一個(gè)實(shí)例,然后再調(diào)用屬性name,age的setter方法,為這兩個(gè)屬性設(shè)值。這應(yīng)該就是最簡(jiǎn)單的依賴注入啦。我們只需要在容器ApplicationContext中通過實(shí)例編號(hào)person去取就可以啦。書上還有一些關(guān)于依賴關(guān)系的一些介紹,加入A組件調(diào)用了B組件的方法,我們可稱A組件依賴于B組件。

依賴注入還有個(gè)特別高大上的名字,控制反轉(zhuǎn)(Inversion of Control,IoC),有木有,是不是很難懂。

Spring推薦面向接口編程,更好地讓規(guī)范和實(shí)現(xiàn)分離,寫代碼會(huì)更愉快……好有道理的樣子,好吧,我會(huì)試試的!!!

下面的示例是注入一個(gè)復(fù)雜屬性,就是將一個(gè)配置好的bean作為另一個(gè)bean的屬性:

src/cn/edu/cqupt/MySpring/bean/Axe.java package cn.edu.cqupt.MySpring.bean; /**斧子,六級(jí)沒過,原諒我不認(rèn)識(shí)這個(gè)單詞*/ public interface Axe {public String chop(); } src/cn/edu/cqupt/MySpring/bean/impl/StoneAxe.java package cn.edu.cqupt.MySpring.bean.impl; import cn.edu.cqupt.MySpring.bean.Axe; public class StoneAxe implements Axe {@Overridepublic String chop() {return "石斧...";} } src/cn/edu/cqupt/MySpring/bean/impl/Person.java(setter、getter都刪掉羅) package cn.edu.cqupt.MySpring.bean.impl; import cn.edu.cqupt.MySpring.bean.Axe; public class Person {private String name;private int age;private Axe axe;public String toString(){return "Name:"+this.name+",age:"+this.age;} } src/beans.xml(部分代碼) <?xml version="1.0" encoding="utf-8"?><bean id="person" class="cn.edu.cqupt.MySpring.bean.impl.Person"><property name="name" value="xiaofeig"/><property name="age" value="20"/><property name="axe" ref="stoneAxe"/> </bean> <bean id="stoneAxe" class="cn.edu.cqupt.MySpring.bean.impl.StoneAxe"/> src/cn/edu/cqupt/MyString/test/TestMain.java package cn.edu.cqupt.MySpring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.cqupt.MySpring.bean.Axe; import cn.edu.cqupt.MySpring.bean.impl.Person; public class TestMain {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");Person person=context.getBean("person",Person.class);Axe axe=person.getAxe();System.out.println(axe.chop());} } 打印結(jié)果:石斧...看這句<property name="axe" ref="stoneAxe"/>,axe表示Person的屬性,stoneAxe是我們配置的是一個(gè)實(shí)例的編號(hào),通過ref這個(gè)標(biāo)簽屬性,我們就可以輕松地將stoneAxe實(shí)例注入到Person的屬性中,看起來是不是也很easy。

我看書到537頁(yè)的時(shí)候,覺得最重要的一點(diǎn)就是依賴注入通過配置的方式將Bean實(shí)例之間代碼層次的耦合分離了出來,我們可以清晰地透過xml文件觀察到Bean實(shí)例之間的依賴關(guān)系。

書上原話總結(jié)了Spring IoC容器的3個(gè)基本要點(diǎn):

  • 面向接口編程可以將各組件之間的耦合提升到接口層次,從而有利項(xiàng)目后期的擴(kuò)展
  • 應(yīng)用程度的各組件不再有程序主動(dòng)產(chǎn)生,而是有Spring容器來負(fù)責(zé)產(chǎn)生、并初始化
  • Spring采用配置文件、或Annotation(注解,書后面會(huì)有提到,不過我總是用不來,新事物的自然抵觸)來管理Bean的實(shí)現(xiàn)類、依賴關(guān)系,Spring容器通過配置文件利用反射來創(chuàng)建實(shí)例(所謂的依賴注入)

可以看出,上面寫的Demo都是Spring容器利用setter方法來為實(shí)例注入屬性值的,其實(shí),我們也可以讓容器利用有參構(gòu)造方法來直接創(chuàng)建一個(gè)實(shí)例,這就是書上提到的構(gòu)造注入。

src/cn/edu/cqupt/MySpring/bean/impl/Person.java(setter、getter都刪掉羅) package cn.edu.cqupt.MySpring.bean.impl; import cn.edu.cqupt.MySpring.bean.Axe; public class Person {private String name;private int age;private Axe axe;public Person(){}public Person(String name,int age,Axe axe){this.name=name;this.age=age;this.axe=axe;}public String toString(){return "Name:"+this.name+",age:"+this.age;} } src/beans.xml <?xml version="1.0" encoding="utf-8"?> <bean id="person" class="cn.edu.cqupt.MySpring.bean.impl.Person"><constructor-arg value="xiaofeig"/><constructor-arg value="20"/><constructor-arg ref="stoneAxe"/> </bean> <bean id="stoneAxe" class="cn.edu.cqupt.MySpring.bean.impl.StoneAxe"/> src/cn/edu/cqupt/MySpring/test/TestMain.java package cn.edu.cqupt.MySpring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.cqupt.MySpring.bean.impl.Person; public class TestMain {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");Person person=context.getBean("person",Person.class);System.out.println(person);System.out.println(person.getAxe().chop());} } 打印結(jié)果: Name:xiaofeig,age:20 石斧...

標(biāo)簽<constructor-arg/>就是用來構(gòu)造注入的哦,標(biāo)簽的順序就是構(gòu)造參數(shù)的順序,上面的實(shí)例比較簡(jiǎn)單,Person類只有一個(gè)有參構(gòu)造方法,第二個(gè)屬性在注入的時(shí)候直接將”20”轉(zhuǎn)換成了整型,現(xiàn)在來假想一種情況,如果Person類還有一個(gè)構(gòu)造方法,如:

打印結(jié)果: Name:xiaofeig,age:0 石斧...

是不是秒懂啊,Spring容器會(huì)優(yōu)先考慮上面的那個(gè)構(gòu)造方法,如果我們想讓Spring容器先調(diào)用含int age的構(gòu)造方法,可以設(shè)置標(biāo)簽property的屬性type值為’int’。

<constructor-arg value="20" type="int"/> hint: specify index/type/name arguments for simple parameters to avoid type ambiguities

好了,簡(jiǎn)單地了解了這兩種構(gòu)造方法,設(shè)值注入和構(gòu)造注入,個(gè)人還是比較喜歡設(shè)值注入啦。

轉(zhuǎn)載于:https://www.cnblogs.com/fei-er-blog/p/4741892.html

總結(jié)

以上是生活随笔為你收集整理的Spring初识的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。