day02-Spring基本介紹02

来源:https://www.cnblogs.com/liyuelian/archive/2023/01/15/17054174.html
-Advertisement-
Play Games

Spring基本介紹02 5.簡單模擬Spring基於XML配置的程式 5.1需求說明 自己寫一個簡單的Spring容器,通過讀取beans.xml,獲取第一個Javabean:Monster的對象,給該對象屬性賦值,放入到容器中,並輸出該對象信息 也就是說,不使用spring原生框架,我們自己簡單 ...


Spring基本介紹02

5.簡單模擬Spring基於XML配置的程式

5.1需求說明

  1. 自己寫一個簡單的Spring容器,通過讀取beans.xml,獲取第一個Javabean:Monster的對象,給該對象屬性賦值,放入到容器中,並輸出該對象信息

  2. 也就是說,不使用spring原生框架,我們自己簡單模擬實現,目的是瞭解Spring容器的簡單機制

5.2思路分析

image-20230115184402488

5.3代碼實現

引入dom4j.jar包

image-20230115185128891

MyApplicationContext.java:

package com.li.myapplicationcontext;

import com.li.bean.Monster;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 李
 * @version 1.0
 * 1. 這個程式用於實現Spring的一個簡單容器機制
 * 2. 後面還會詳細的實現
 * 3. 這裡我們主要實現的是如何將beans.xml文件進行解析,並生成對象,放入容器中
 * 4. 然後提供一個getBean(id) 返回對應的對象
 */
public class MyApplicationContext {
    private ConcurrentHashMap<String, Object> singletonObjects
            = new ConcurrentHashMap<>();

    //構造器
    //接收一個容器的配置文件,如 beans.xml,預設路徑在src下
    public MyApplicationContext(String iocBeanXMLFile) throws Exception {
        //1.得到配置文件的類載入路徑
        String path = this.getClass().getResource("/").getPath();

        //2.創建 saxReader
        SAXReader saxReader = new SAXReader();

        //3.得到document對象
        Document document = saxReader.read(new File(path + iocBeanXMLFile));

        //4.得到rootDocument
        Element rootElement = document.getRootElement();

        //5.得到第一個bean-monster01
        Element bean = (Element) rootElement.elements("bean").get(0);

        //6.獲取到第一個bean的相關屬性(真實的情況下會把屬性保存到beanDefinitionMap中)
        String id = bean.attributeValue("id");//bean的id
        String classFullPath = bean.attributeValue("class");//類的全路徑
        List<Element> property = bean.elements("property");
        //原本是遍歷,這裡為了簡化,就直接獲取
        //property的value值
        Integer monsterId =
                Integer.parseInt(property.get(0).attributeValue("value"));
        String name = property.get(1).attributeValue("value");
        String skill = property.get(2).attributeValue("value");

        //7.反射創建對象
        Class<?> aClass = Class.forName(classFullPath);
        //這裡的 o對象就是Monster對象
        Monster o = (Monster) aClass.newInstance();
        //給對象賦值-這裡為了簡化,直接賦值(真實情況下會使用反射)
        o.setMonsterId(monsterId);
        o.setName(name);
        o.setSkill(skill);

        //8.將創建好的對象放到singletonObjects單例對象池中
        singletonObjects.put(id, o);
    }

    public Object getBean(String id) {
        return singletonObjects.get(id);
    }
}

Test.java用於測試:

package com.li.myapplicationcontext;


import com.li.bean.Monster;

/**
 * @author 李
 * @version 1.0
 */
public class Test {
    public static void main(String[] args) throws Exception {
        MyApplicationContext ioc = new MyApplicationContext("beans.xml");
        Monster monster01 = (Monster) ioc.getBean("monster01");
        System.out.println("monster01=" + monster01);
        System.out.println("monsterId=" + monster01.getMonsterId() +
                " name=" + monster01.getName() +
                " skill=" + monster01.getSkill());
    }
}
image-20230115193702842

6.Spring原生容器底層結構梳理

我們之前在Spring基本介紹01--4.5Spring容器的結構/機制有一些基礎的分析,現在來梳理一下:

7.練習

7.1關於bean的id

如下,在beans.xml中,我們註入2個Monster對象,但是不指定bean的id

<bean class="com.li.bean.Monster">
    <property name="monsterId" value="100"/>
    <property name="name" value="牛魔王"/>
    <property name="skill" value="芭蕉扇"/>
</bean>

<bean class="com.li.bean.Monster">
    <property name="monsterId" value="200"/>
    <property name="name" value="紅孩兒"/>
    <property name="skill" value="三昧真火"/>
</bean>

問題1:運行會不會報錯?

答:不會報錯,可以正常運行。

問題2:如果不報錯,你是否能找到分配的id並獲取該對象?

答:系統會預設分配id,分配id的規則是:全類名#0,全類名#1......全類名#n,這樣的規則來分配id

我們可以通過debug的方式來查看:

beanFactory.beanDefinitionMap.table:

image-20230115205901982
package com.li.homework;

import com.li.bean.Monster;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

/**
 * @author 李
 * @version 1.0
 */
public class Homework01 {
    @Test
    public void getMonster() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");

        Monster monster01 = ioc.getBean("com.li.bean.Monster#0", Monster.class);
        System.out.println("monster01=" + monster01);
        System.out.println("monsterId=" + monster01.getMonsterId());

        Monster monster02 = ioc.getBean("com.li.bean.Monster#1", Monster.class);
        System.out.println("monster02=" + monster02);
        System.out.println("monsterId=" + monster02.getMonsterId());

        System.out.println("ok~~");
    }
}
image-20230115205336892

在實際開發中不會省略bean的id

7.2練習2

創建一個Car類(屬性:id,name,price),具體要求如下:

  1. 創建ioc容器文件(即配置文件),並配置一個Car對象(bean)
  2. 通過java程式到ioc容器獲取該bean對象,並輸出

Car:

package com.li.bean;

/**
 * @author 李
 * @version 1.0
 */
public class Car {
    private Integer id;
    private String name;
    private Double price;

    public Car() {
    }

    public Car(Integer id, String name, Double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

beans2.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 class="com.li.bean.Car" id="car01">
        <property name="id" value="10001"/>
        <property name="name" value="寶馬"/>
        <property name="price" value="1230000"/>
    </bean>
</beans>

Homework02:

package com.li.homework;

import com.li.bean.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

/**
 * @author 李
 * @version 1.0
 */
public class Homework02 {
    @Test
    public void getCart() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans2.xml");
        Car car = ioc.getBean("car01", Car.class);
        System.out.println(car);
    }
}
image-20230115212120113
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 按鈕: <el-button id="manyou" @click="Ismovement" type="primary" >漫游模式</el-button> 樣式: /* 逗號表示A,B兩個標簽同時擁有大括弧中的CSS樣式 */ .el-button--primary.is-active, /* ...
  • 事件迴圈機制 同步與非同步 我們先思考兩個問題,如下: 為什麼會存在同步和非同步的概念? 我們的JavaScript是單線程的,也就是我們的工作流水線的只有一條。如果我們的任務全放在流水線上,其中一個任務出現問題就會阻塞後面的任務,導致我們的工作流水線卡住。因此為了更加高效合理利用這條流水線,在Java ...
  • 前言 這篇博文續接的是 UML建模、設計原則、創建型設計模式、行為型設計模式,有興趣的可以看一下 3.3、結構型 這些設計模式關註類和對象的組合。將類和對象組合在一起,從而形成更大的結構 * 3.3.1、proxy 代理模式 定義:為某對象提供一種代理以控制對該對象的訪問。即:客戶端通過代理間接地訪 ...
  • 面向對象編程(OOP) 屬性+方法=類 面向過程 步驟清晰簡單, 第一步做什麼, 第二步做什麼... 適用於處理簡單的問題 面向對象 物以類聚和分類的思想模式 思考解決問題需要做出哪些分類, 然後對這些分類進行單獨思考和研究 最後,將分類下的細節進行了面向過程的研究 面向對象適用於複雜問題, 適合處 ...
  • C++|變數 前言 在C++編程中,需要用到很多種變數 本文將詳談幾種常見變數 如有錯誤,歡迎指出 零、變數格式 定義並賦值 數據類型 變數名=值; 定義 數據類型 變數名; 賦值 變數名=值; 輸入 cin>>變數名; 輸出 cout<<變數名; 一、數字數據類型 如圖所示,雖然 C++ 提供了許 ...
  • 實踐環境 Odoo 14.0-20221212 (Community Edition) web_responsive-14.0.1.2.1.zip https://apps.odoo.com/apps/modules/14.0/web_responsive/ 操作步驟 1、把下載的web_respo ...
  • 1 簡介 Cloud SQL 是GCP上的關係型資料庫,常用的有三種方式來創建: (1) 界面操作 (2) 命令行 gcloud (3) Terraform 在開始之前,可以查看:《初始化一個GCP項目並用gcloud訪問操作》。 2 GCP 操作界面 登陸GCP,選擇SQL,可以創建MySQL、P ...
  • 在上一篇博客中我們有提到一個詞叫做常量,現在就來講講它常量:指的是在程式運行過程中值不會發生改變的量其實我們也有寫過,在這個輸出語句中,這個1就是常量簡單來說程式運行下去,這個1它怎麼樣也不會變成3吧變數:指的是在程式運行過程中值會發生改變的量那麼怎麼來定義一個變數呢我們先來試著定義一個x,值就先為 ...
一周排行
    -Advertisement-
    Play Games
  • 就像 Web Api 介面可以對入參進行驗證,避免用戶傳入非法的或者不符合我們預期的參數一樣,選項也可以對配置源的內容進行驗證,避免配置中的值與選項類中的屬性不對應或者不滿足預期,畢竟大部分配置都是通過字元串的方式,驗證是很有必要的。 1. 註解驗證 像入參驗證一樣,選項驗證也可以通過特性註解方便地 ...
  • 原文作者:aircraft 原文鏈接:https://www.cnblogs.com/DOMLX/p/17270107.html 加工的泛型類如下: using System; using System.Collections.Generic; using System.IO; using Syst ...
  • 在前一篇文章,我們瞭解瞭如何通過.NET6+Quartz開發基於控制台應用程式的定時任務,今天繼續在之前的基礎上,進一步講解基於ASP.NET Core MVC+Quartz實現定時任務的可視化管理頁面,僅供學習分享使用,如有不足之處,還請指正。 涉及知識點 Quartz組件,關於Quartz組件的 ...
  • 面向對象1 面向對象,更在乎的結果,而過程的實現並不重要 IDea快捷鍵(基礎版) | 快捷鍵 | 作用 | | | | | ctrl + / | 快捷註釋 | | ctrl + shift + / | 多行註釋 | | ctrl + d | 快速複製 | | ctrl + shift + up/d ...
  • NX中的checkmate功能是用於檢查模型、圖紙數據的工具,在UGOPEN中有例子。手動操作可以檢查已載入的裝配下所有零部件,可以設置通過後保存模型,檢查結果保存到Teamcenter中,預設保存在零組件版本下。 代碼中可以設置多個檢查規則。相關設置可以在用戶預設設置中進行設置。 1 // 2 / ...
  • JavaSE 運算符 算術運算符:+,-,*,/,%,++(自增),--(自減) i++:先用後+1;++i:先+1後用 賦值運算符:= 擴展賦值運算符:+=,-=,*=,/= a+=b >a=a+b: ​ 可讀性差,但是編譯效率高,且會自動進行類型轉換; ​ 當ab為基本數據類型時,a+b和b+a ...
  • 面向對象2 訪問修飾符 | | private | default | protected | public | | | | | | | | 當前類 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_che ...
  • 推薦一些學習qml教程 Qt官方的QML教程: https://doc.qt.io/qt-5/qtqml-index.html 這是一個由Qt官方提供的完整的QML教程,包含了所有基本知識和高級語法。 QML中文網:http://www.qmlcn.com/ 這是一個非常不錯的中文QML學習網站,提 ...
  • QAbstractBUtton: 所有按鈕控制項的基類 提供按鈕的通用功能 繼承自QWidget 屬於抽象類別,不能直接去使用,必須藉助於子類(除非你覺得子類不夠用,想自定義一個按鈕) 大部分功能之前已經使用過,在這裡只作簡單介紹 文本設置: setText(str) :設置按鈕提示文本 text() ...
  • 使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇介紹 VLD 配置文件中配置項 StartDisabled 的使用方法。 ...