<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ByxContainer輕量級(jí) IOC 容器

          聯(lián)合創(chuàng)作 · 2023-09-30 06:54

          ByxContainer是一個(gè)輕量級(jí)IOC容器,具有以下特性:

          • 使用Json格式的配置文件,拋棄復(fù)雜冗長(zhǎng)的xml語法
          • 支持構(gòu)造函數(shù)注入、靜態(tài)工廠注入、實(shí)例工廠注入、JavaBean屬性注入、setter注入、條件注入
          • 支持根據(jù)id或類型獲取容器中的對(duì)象
          • 支持靈活的對(duì)象創(chuàng)建方式的配置
          • 對(duì)象的延遲加載和單例

          在項(xiàng)目中引入ByxContainer

          1. 添加maven倉(cāng)庫地址

            <repositories>
                <repository>
                    <id>byx-maven-repo</id>
                    <name>byx-maven-repo</name>
                    <url>https://gitee.com/byx2000/maven-repo/raw/master/</url>
                </repository>
            </repositories>
          2. 添加maven依賴

            <dependencies>
                <dependency>
                    <groupId>byx.container</groupId>
                    <artifactId>ByxContainer</artifactId>
                    <version>1.0.0</version>
                </dependency>
            </dependencies>

          ByxContainer的配置文件

          ByxContainer使用Json作為配置文件的格式。你可以將配置文件命名為任何名字,放在任何你喜歡的路徑下。

          ByxContainer配置文件的基本框架如下:

          {
              "$schema": "http://byx2000.gitee.io/byxcontainer/schema/schema.json",
              "typeAlias":
              {
                  
              },
              "components":
              {
                  
              }
          }
          
          • typeAlias用于配置類型別名
          • components用于聲明容器中的所有組件
          • $schema鍵指定JSON的約束文件路徑,使用約束文件可以在編輯JSON文件時(shí)啟用自動(dòng)完成、語法檢查等功能。某些編輯器可能需要特殊設(shè)置才能支持JSON約束文件的識(shí)別

          ByxContainer的所有組件都定義在components中。組件以鍵值對(duì)的形式寫在components對(duì)象中,鍵就是組件的id,值就是組件的定義。

          {
              "components":
              {
                  "c1": ... // 組件c1的定義
                  "c2": ... // 組件c2的定義
                  "c3": ... // 組件c3的定義
                  ...
              }
          }
          

          加載與使用ByxContainer

          在應(yīng)用程序的初始化代碼中,按照以下方式加載配置文件,并初始化容器:

          InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("配置文件路徑");
          ContainerFactory factory = new JsonContainerFactory(inputStream);
          Container container = factory.create();

          容器初始化完成后,有兩種方法來獲取某個(gè)已注冊(cè)的對(duì)象:

          • 通過注冊(cè)時(shí)的id獲取
            // 獲取id為c的對(duì)象
            SomeType c = container.getObject("c");
          • 通過類型獲取
            // 獲取容器中類型為SomeType的對(duì)象
            SomeType c = container.getObject(SomeType.class);

          在以下三種情況時(shí),會(huì)拋出ByxContainerExceprion異常:

          • 指定id的對(duì)象不存在
          • 指定類型的對(duì)象不存在
          • 有多個(gè)指定類型的對(duì)象

          更多使用說明請(qǐng)看軟件文檔:https://github.com/byx2000/ByxContainer

          瀏覽 27
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产无码精品黄色电影 | 91啦丨国产丨人妻白洁 | 免费看日本黄色一级片 | a在线级电影网站 | 国产成人无码毛片 |