1. 程式人生 > 其它 >spring中基於xml的Ioc容器

spring中基於xml的Ioc容器

技術標籤:Spring

IOC
在這裡插入圖片描述

明確 ioc 的作用
削減計算機程式的耦合(解除我們程式碼中的依賴關係

Ioc容器

BeanFactory
BeanFactory 是基礎型別的 IoC 容器,它由 org.springframework.beans.facytory.BeanFactory 介面定義,並提供了完整的 IoC 服務支援。簡單來說,BeanFactory 就是一個管理 Bean 的工廠,它主要負責初始化各種 Bean,並呼叫它們的生命週期方法。
ApplicationContext
ApplicationContext 是 BeanFactory 的子介面,也被稱為應用上下文。該介面的全路徑為 org.springframework.context.ApplicationContext,它不僅提供了 BeanFactory 的所有功能,還添加了對 i18n(國際化)、資源訪問、事件傳播等方面的良好支援。

ApplicationContext 介面有兩個常用的實現類,具體如下。
1)ClassPathXmlApplicationContext
該類從類路徑 ClassPath 中尋找指定的 XML 配置檔案,找到並裝載完成 ApplicationContext 的例項化工作,具體如下所示。

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(String configLocation);

在上述程式碼中,configLocation 引數用於指定 Spring 配置檔案的名稱和位置,如 applicationContext.xml。

2)FileSystemXmlApplicationContext
該類從指定的檔案系統路徑中尋找指定的 XML 配置檔案,找到並裝載完成 ApplicationContext 的例項化工作,具體如下所示。

ApplicationContext applicationContext = new FileSystemXmlApplicationContext(String configLocation);

它與 ClassPathXmlApplicationContext 的區別是:在讀取 Spring 的配置檔案時,FileSystemXmlApplicationContext 不再從類路徑中讀取配置檔案,而是通過引數指定配置檔案的位置,它可以獲取類路徑之外的資源,如“F:/workspaces/applicationContext.xml”。

BeanFactory 和 ApplicationContext 的區別:

建立物件的時間點不一樣。
ApplicationContext:只要一讀取配置檔案,預設情況下就會建立物件。
BeanFactory:什麼使用什麼時候建立物件。

1建立一個maven工程

準備好dao和service

2.匯入spring依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>
    </dependencies>


</project>

3.在resources下建立bean.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- bean 標籤:用於配置讓 spring 建立物件,並且存入 ioc 容器之中
id 屬性:物件的唯一標識。
class 屬性:指定要建立物件的全限定類名
-->
    <!-- 配置 service -->
    <bean id="accountService" class="com.service.impl.AccountServiceImpl">
    </bean>
    <!-- 配置 dao -->
    <bean id="accountDao" class="com.dao.impl.AccountDaoImpl"></bean>
</beans>

4.測試

package com.ui;

import com.dao.IAccountDao;
import com.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 模擬一個表現層,用於呼叫業務層
 */
public class Client {

    public static void main(String[] args) {
    //1.使用 ApplicationContext 介面,就是在獲取 spring 容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2.根據 bean 的 id 獲取物件
        IAccountService aService = (IAccountService) ac.getBean("accountService");
        System.out.println(aService);
        IAccountDao aDao = (IAccountDao) ac.getBean("accountDao");
        System.out.println(aDao);
    }
}