javascript
SpringIOC容器介绍
IOC & DI 概述
配置 bean
配置形式:基于 XML 文件的方式;基于注解的方式
Bean 的配置方式:通過全類名(反射)、通過工廠方法(靜態工廠方法 & 實例工廠方法)、FactoryBean
IOC 容器 BeanFactory & ApplicationContext 概述
依賴注入的方式:屬性注入;構造器注入
注入屬性值細節
自動裝配
bean 之間的關系:繼承;依賴
bean 的作用域:singleton;prototype;WEB 環境作用域
使用外部屬性文件
IOC 容器中 Bean 的生命周期
?
在 Spring 的 IOC 容器里配置 Bean
在 xml 文件中通過 bean 節點來配置 bean
id:Bean 的名稱。
在 IOC 容器中必須是唯一的
若 id 沒有指定,Spring 自動將權限定性類名作為 Bean 的名字
id 可以指定多個名字,名字之間可用逗號、分號、或空格分隔
?
Spring 容器
在 Spring IOC 容器讀取 Bean 配置創建 Bean 實例之前, 必須對它進行實例化. 只有在容器實例化后, 才可以從 IOC 容器里獲取 Bean 實例并使用.
Spring 提供了兩種類型的 IOC 容器實現.?
BeanFactory: IOC 容器的基本實現.
ApplicationContext: 提供了更多的高級特性. 是 BeanFactory 的子接口.
BeanFactory 是 Spring 框架的基礎設施,面向 Spring 框架本身;ApplicationContext 面向使用 Spring 框架的開發者,幾乎所有的應用場合都直接使用 ApplicationContext 而非底層的 BeanFactory
無論使用何種方式, 配置文件是相同的.
?
ApplicationContext
ApplicationContext 的主要實現類:
ClassPathXmlApplicationContext:從 類路徑下加載配置文件
FileSystemXmlApplicationContext: 從文件系統中加載配置文件
ConfigurableApplicationContext 擴展于 ApplicationContext,新增加兩個主要方法:refresh() 和 close(), 讓 ApplicationContext 具有啟動、刷新和關閉上下文的能力
ApplicationContext 在所有單例的 Bean。初始化上下文時就實例化
WebApplicationContext 是專門為 WEB 應用而準備的,它允許從相對于 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的唯一標識class:指定全類名 反射的方式創建對象: Class cls = Class.forName("com.learn.spring.beans.HelloWorld")Object obj = cls.newInstance(); // 需要提供默認的構造器.property: 通過set方法給指定的屬性賦值--><bean id="helloWorld" class="com.learn.spring.beans.HelloWorld"><property name="name1" value="Jerry"></property></bean><!-- 目前的配置,SpringIOC容器在實例化的時候都會創建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,注意:如果有多個類型兼容的bean,會有問題.//HelloWorld helloWorld = ctx.getBean(HelloWorld.class);HelloWorld helloWorld = ctx.getBean("helloWorld",HelloWorld.class);//3.調用方法helloWorld.sayHello(); } }?
總結
以上是生活随笔為你收集整理的SpringIOC容器介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOCDI概述
- 下一篇: 依赖注入_set方法注入_构造器注入