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
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...