# spring 第一天- [ ] ## 1.在maven項(xiàng)目中添加依賴 ```org.springframeworkspring-context5.0.2.RELEASE``` ?- [ ] ## 2.創(chuàng)建xml文件放在resource下面隶症,配置bean ```?xml version="1.0" encoding="UTF-8"?``` ?- [ ] ## 3.實(shí)例化上下文 ``` ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config.xml"); ``` ?- [ ] ## 4.getBean等到對(duì)象 ``` Student student = (Student) applicationContext.getBean("studentbean"); ``` # name - [ ] 1.分隔符是空格扰才,逗號(hào),分號(hào) - [ ] 2.分隔符能混用 ```------------------------------------------------------------------------- Student stu = (Student) applicationContext.getBean("student"); Student stu1 = (Student) applicationContext.getBean("stu"); Student stu2 = (Student) applicationContext.getBean("stud"); ``` ? - [ ] 3.多個(gè)bean的name配置不能相同 ## 配置的3種方式 - [ ] 1.直接class配置通今,實(shí)例化的是class代表的對(duì)象 `````` - [ ] 2.工廠方法:factory-method factory-bean factory-method factory-bean `````` ``` package bean; public class StudentFactory { public static Student getclass(){ return new Student(); } public Student getclass2(){ return new Student(); } } ``` ? 3.FactoryBean ``` package bean; import org.springframework.beans.factory.FactoryBean; import org.springframework.lang.Nullable; public class TeacherFactoryBean implements FactoryBean{ @Nullable public Teacher getObject() throws Exception { return new Teacher(); } @Nullable public Class?getObjectType() { return Teacher.class; } } ``` ? # 作用域 Singleton :由容器管理對(duì)象的生命周期,也就是說(shuō)容器存在饥悴,對(duì)象就創(chuàng)建出來(lái)(設(shè)定的init方法會(huì)執(zhí)行故慈,容器銷毀,管理的對(duì)象也會(huì)銷毀 ``` (設(shè)定的銷毀方法會(huì)執(zhí)行) ``` Prototype:getBean的臨時(shí)產(chǎn)生一個(gè)阅酪,產(chǎn)生之后的對(duì)象旨袒,spring容器就不管理,由程序自己去管理它的銷毀 全局的配置:default-xxxx 調(diào)用銷毀方法 ``` ((ConfigurableApplicationContext)applicationContext).close(); ``` ? - [ ] 1术辐、singleton 作用域 當(dāng)一個(gè)bean的 作用域設(shè)置為singleton, 那么Spring IOC容器中只會(huì)存在一個(gè)共享的bean實(shí)例砚尽,并且所有對(duì)bean的請(qǐng)求,只要id與該bean定義相匹配辉词,則只會(huì)返回bean的同一實(shí)例必孤。換言之,當(dāng)把 一個(gè)bean定義設(shè)置為singleton作用域時(shí)瑞躺,Spring IOC容器只會(huì)創(chuàng)建該bean定義的唯一實(shí)例敷搪。這個(gè)單一實(shí)例會(huì)被存儲(chǔ)到單例緩存(singleton cache)中,并且所有針對(duì)該bean的后續(xù)請(qǐng)求和引用都 將返回被緩存的對(duì)象實(shí)例幢哨,這里要注意的是singleton作用域和GOF設(shè)計(jì)模式中的單例是完全不同的赡勘,單例設(shè)計(jì)模式表示一個(gè)ClassLoader中 只有一個(gè)class存在,而這里的singleton則表示一個(gè)容器對(duì)應(yīng)一個(gè)bean捞镰,也就是說(shuō)當(dāng)一個(gè)bean被標(biāo)識(shí)為singleton時(shí) 候闸与,spring的IOC容器中只會(huì)存在一個(gè)該bean顽悼。 配置實(shí)例: ```或者``` - [ ] 2、prototype prototype作用域部署的bean几迄,每一次請(qǐng)求(將其注入到另一個(gè)bean中蔚龙,或者以程序的方式調(diào)用容器的 getBean()方法)都會(huì)產(chǎn)生一個(gè)新的bean實(shí)例,相當(dāng)與一個(gè)new的操作映胁,對(duì)于prototype作用域的bean木羹,有一點(diǎn)非常重要,那就是Spring不能對(duì)一個(gè)prototype bean的整個(gè)生命周期負(fù)責(zé)解孙,容器在初始化坑填、配置、裝飾或者是裝配完一個(gè)prototype實(shí)例后弛姜,將它交給客戶端脐瑰,隨后就對(duì)該prototype實(shí)例不聞不問(wèn)了。不管何種作用域廷臼,容器都會(huì)調(diào)用所有對(duì)象的初始化生命周期回調(diào)方法苍在,而對(duì)prototype而言,任何配置好的析構(gòu)生命周期回調(diào)方法都將不會(huì)被調(diào)用荠商。 清除prototype作用域的對(duì)象并釋放任何prototype bean所持有的昂貴資源寂恬,都是客戶端代碼的職責(zé)。(讓Spring容器釋放被singleton作用域bean占用資源的一種可行方式是莱没,通過(guò)使用 bean的后置處理器初肉,該處理器持有要被清除的bean的引用。) 配置實(shí)例: ```或者 ```
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? # 多配置文件
? ? ? ? ? ? ? ? ? 1 import
? ? ? ? ? ? ? ? ? 2 new xxx("","")
? ? ? ? ? ? ? ? ? 驗(yàn)證:每個(gè)文件里面配置一個(gè)bean
? ? ? ? ? ? ? ? ? getBean的時(shí)候都能得到