spring 官網(wǎng)學(xué)習(xí)spring 知識

spring ioc container

只是轉(zhuǎn)載bean的容器乡革,

IoC , DI,

DI的方式

  • through constructor arguments 構(gòu)造函數(shù)
  • arguments to a factory method, 工廠方式
  • properties that are set, 屬性注入

IoC 容器

  • BeanFactory
  • ApplicationContext
  • WebApplicationCotext
  • DefaultListableBeanFactory
  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext

ApplicationContext與BeanFactory的區(qū)別

  • Easier integration with Spring’s AOP features尼斧,aop特性
  • Message resource handling (for use in internationalization), 消息處理益楼,主要是國際化
  • Event publication , 事件發(fā)布, ApplicationEvent
  • Application-layer specific contexts such as the WebApplicationContext for use in web applications. 擴展web的容器

spring ioc container

image.png

構(gòu)建應(yīng)用上下文的方式

  • xml
  • java
  • java config
xml如何構(gòu)建上下文
  1. 編寫xml文件---services.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

  1. 使用ClassPathXmlApplicationContext
// 支持多個xml資源, 拼接上去就可以了
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

//  構(gòu)建2: classpath, 這個文件在resource文件夾下
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:services.xml");

另一種寫法

GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();
java config構(gòu)建
ApplicationContext context = new AnnotationConfigApplicationContext();

后臺會將該類所在的路徑作為基礎(chǔ)掃描路徑粘咖,當(dāng)然包括本類會被掃描進章办。 
還會掃描對應(yīng)的注解@Configuration, @Bean, @Component, @Import, @DependsOn

另一種寫法

public class JavaConfigTest {
    public static void main(String[] arg) {
     AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
     ctx.register(AppConfig.class); // 將本類注入到IoC中
      ctx.refresh(); // 刷新上下文带膜,掃描并讀取對應(yīng)的class到beanDefinition中批狐,完成上下文構(gòu)建的工作
}
}

配置元數(shù)據(jù)

  • Java-based configuration:
    @Configuration, @Bean, @Import, and @DependsOn annotations.
  • Annotation-based configuration:
    @Autowired,@Resource, @Inject
  • xml properties
    <beans>, <bean>,
    <property name="accountDao" ref="accountDao"/>
    <import resource="resources/messageSource.xml"/>

例子

<beans>
// 注入其他xml文件
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>

 <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
// 注入其他屬性
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>
</beans>

獲取Bean

簡答方式就是通過getBean的方式

例子

T getBean(String name, Class<T> requiredType)
--------------------------------------------------------

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance
List<String> userList = service.getUsernameList();

寫于 2020-08-12 23:42:11

PS: 若你覺得可以扇售、還行前塔、過得去、甚至不太差的話承冰,可以“關(guān)注”一下华弓,就此謝過!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市困乒,隨后出現(xiàn)的幾起案子寂屏,更是在濱河造成了極大的恐慌,老刑警劉巖娜搂,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件迁霎,死亡現(xiàn)場離奇詭異,居然都是意外死亡百宇,警方通過查閱死者的電腦和手機考廉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恳谎,“玉大人芝此,你說我怎么就攤上這事∫蛲矗” “怎么了婚苹?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鸵膏。 經(jīng)常有香客問我膊升,道長,這世上最難降的妖魔是什么谭企? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任廓译,我火速辦了婚禮,結(jié)果婚禮上债查,老公的妹妹穿的比我還像新娘非区。我一直安慰自己,他們只是感情好盹廷,可當(dāng)我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布征绸。 她就那樣靜靜地躺著,像睡著了一般俄占。 火紅的嫁衣襯著肌膚如雪管怠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天缸榄,我揣著相機與錄音渤弛,去河邊找鬼。 笑死甚带,一個胖子當(dāng)著我的面吹牛她肯,可吹牛的內(nèi)容都是我干的佳头。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼辕宏,長吁一口氣:“原來是場噩夢啊……” “哼畜晰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瑞筐,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎腊瑟,沒想到半個月后聚假,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡闰非,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年膘格,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片财松。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡瘪贱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辆毡,到底是詐尸還是另有隱情菜秦,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布舶掖,位于F島的核電站球昨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏眨攘。R本人自食惡果不足惜主慰,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鲫售。 院中可真熱鬧共螺,春花似錦、人聲如沸情竹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鲤妥。三九已至佳吞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間棉安,已是汗流浹背底扳。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留贡耽,地道東北人衷模。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓鹊汛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親阱冶。 傳聞我的和親對象是個殘疾皇子刁憋,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容