javascript
SpringIOC容器介绍
IOC & DI 概述
配置 bean
配置形式:基于 XML 文件的方式;基于注解的方式
Bean 的配置方式:通過全類名(反射)、通過工廠方法(靜態(tài)工廠方法 & 實(shí)例工廠方法)、FactoryBean
IOC 容器 BeanFactory & ApplicationContext 概述
依賴注入的方式:屬性注入;構(gòu)造器注入
注入屬性值細(xì)節(jié)
自動(dòng)裝配
bean 之間的關(guān)系:繼承;依賴
bean 的作用域:singleton;prototype;WEB 環(huán)境作用域
使用外部屬性文件
IOC 容器中 Bean 的生命周期
?
在 Spring 的 IOC 容器里配置 Bean
在 xml 文件中通過 bean 節(jié)點(diǎn)來配置 bean
id:Bean 的名稱。
在 IOC 容器中必須是唯一的
若 id 沒有指定,Spring 自動(dòng)將權(quán)限定性類名作為 Bean 的名字
id 可以指定多個(gè)名字,名字之間可用逗號(hào)、分號(hào)、或空格分隔
?
Spring 容器
在 Spring IOC 容器讀取 Bean 配置創(chuàng)建 Bean 實(shí)例之前, 必須對它進(jìn)行實(shí)例化. 只有在容器實(shí)例化后, 才可以從 IOC 容器里獲取 Bean 實(shí)例并使用.
Spring 提供了兩種類型的 IOC 容器實(shí)現(xiàn).?
BeanFactory: IOC 容器的基本實(shí)現(xiàn).
ApplicationContext: 提供了更多的高級(jí)特性. 是 BeanFactory 的子接口.
BeanFactory 是 Spring 框架的基礎(chǔ)設(shè)施,面向 Spring 框架本身;ApplicationContext 面向使用 Spring 框架的開發(fā)者,幾乎所有的應(yīng)用場合都直接使用 ApplicationContext 而非底層的 BeanFactory
無論使用何種方式, 配置文件是相同的.
?
ApplicationContext
ApplicationContext 的主要實(shí)現(xiàn)類:
ClassPathXmlApplicationContext:從 類路徑下加載配置文件
FileSystemXmlApplicationContext: 從文件系統(tǒng)中加載配置文件
ConfigurableApplicationContext 擴(kuò)展于 ApplicationContext,新增加兩個(gè)主要方法:refresh() 和 close(), 讓 ApplicationContext 具有啟動(dòng)、刷新和關(guān)閉上下文的能力
ApplicationContext 在所有單例的 Bean。初始化上下文時(shí)就實(shí)例化
WebApplicationContext 是專門為 WEB 應(yīng)用而準(zhǔn)備的,它允許從相對于 WEB 根目錄的路徑中完成初始化工作
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- id:bean的唯一標(biāo)識(shí)class:指定全類名 反射的方式創(chuàng)建對象: Class cls = Class.forName("com.learn.spring.beans.HelloWorld")Object obj = cls.newInstance(); // 需要提供默認(rèn)的構(gòu)造器.property: 通過set方法給指定的屬性賦值--><bean id="helloWorld" class="com.learn.spring.beans.HelloWorld"><property name="name1" value="Jerry"></property></bean><!-- 目前的配置,SpringIOC容器在實(shí)例化的時(shí)候都會(huì)創(chuàng)建bean對象. scope="singleton"--><bean id="helloWorld1" class="com.learn.spring.beans.HelloWorld" scope="singleton"><property name="name1" value="Jerry"></property></bean></beans> package com.learn.spring.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.learn.spring.beans.Book; import com.learn.spring.beans.Car; import com.learn.spring.beans.HelloWorld; import com.learn.spring.beans.Person; import com.learn.spring.beans.PersonList; import com.learn.spring.beans.PersonMap;public class Main {public static void main(String[] args) {/*//1.new 對象HelloWorld helloWorld = new HelloWorld();//2.給name屬性賦值helloWorld.setName("Tom");*///1.獲取IOC容器ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2.從IOC容器中獲取對象.//HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");//通過class去匹配bean,注意:如果有多個(gè)類型兼容的bean,會(huì)有問題.//HelloWorld helloWorld = ctx.getBean(HelloWorld.class);HelloWorld helloWorld = ctx.getBean("helloWorld",HelloWorld.class);//3.調(diào)用方法helloWorld.sayHello(); } }?
總結(jié)
以上是生活随笔為你收集整理的SpringIOC容器介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOCDI概述
- 下一篇: SpringAOP概念