模擬Spring源碼思想,手寫源碼,理解注解

1、BeanDefinition

package com.csdn.myspring;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class BeanDefinition {private String beanName;private Class beanClass;
}

2、掃描包的工具類MyTools

package com.csdn.myspring;
import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class MyTools {public static Set<Class<?>> getClasses(String pack) {// 第一個class類的集合Set<Class<?>> classes = new LinkedHashSet<Class<?>>();// 是否循環迭代boolean recursive = true;// 獲取包的名字 并進行替換String packageName = pack;String packageDirName = packageName.replace('.', '/');// 定義一個枚舉的集合 并進行循環來處理這個目錄下的thingsEnumeration<URL> dirs;try {dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);// 循環迭代下去while (dirs.hasMoreElements()) {// 獲取下一個元素URL url = dirs.nextElement();// 得到協議的名稱String protocol = url.getProtocol();// 如果是以文件的形式保存在服務器上if ("file".equals(protocol)) {// 獲取包的物理路徑String filePath = URLDecoder.decode(url.getFile(), "UTF-8");// 以文件的方式掃描整個包下的文件 并添加到集合中findClassesInPackageByFile(packageName, filePath, recursive, classes);} else if ("jar".equals(protocol)) {// 如果是jar包文件// 定義一個JarFileSystem.out.println("jar類型的掃描");JarFile jar;try {// 獲取jarjar = ((JarURLConnection) url.openConnection()).getJarFile();// 從此jar包 得到一個枚舉類Enumeration<JarEntry> entries = jar.entries();findClassesInPackageByJar(packageName, entries, packageDirName, recursive, classes);} catch (IOException e) {// log.error("在掃描用戶定義視圖時從jar包獲取文件出錯");e.printStackTrace();}}}} catch (IOException e) {e.printStackTrace();}return classes;}private static void findClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries, String packageDirName, final boolean recursive, Set<Class<?>> classes) {// 同樣的進行循環迭代while (entries.hasMoreElements()) {// 獲取jar里的一個實體 可以是目錄 和一些jar包里的其他文件 如META-INF等文件JarEntry entry = entries.nextElement();String name = entry.getName();// 如果是以/開頭的if (name.charAt(0) == '/') {// 獲取后面的字符串name = name.substring(1);}// 如果前半部分和定義的包名相同if (name.startsWith(packageDirName)) {int idx = name.lastIndexOf('/');// 如果以"/"結尾 是一個包if (idx != -1) {// 獲取包名 把"/"替換成"."packageName = name.substring(0, idx).replace('/', '.');}// 如果可以迭代下去 并且是一個包if ((idx != -1) || recursive) {// 如果是一個.class文件 而且不是目錄if (name.endsWith(".class") && !entry.isDirectory()) {// 去掉后面的".class" 獲取真正的類名String className = name.substring(packageName.length() + 1, name.length() - 6);try {// 添加到classesclasses.add(Class.forName(packageName + '.' + className));} catch (ClassNotFoundException e) {// .error("添加用戶自定義視圖類錯誤 找不到此類的.class文件");e.printStackTrace();}}}}}}private static void findClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set<Class<?>> classes) {// 獲取此包的目錄 建立一個FileFile dir = new File(packagePath);// 如果不存在或者 也不是目錄就直接返回if (!dir.exists() || !dir.isDirectory()) {// log.warn("用戶定義包名 " + packageName + " 下沒有任何文件");return;}// 如果存在 就獲取包下的所有文件 包括目錄File[] dirfiles = dir.listFiles(// 自定義過濾規則 如果可以循環(包含子目錄) 或則是以.class結尾的文件(編譯好的java類文件)(file)-> {return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));});// 循環所有文件for (File file : dirfiles) {// 如果是目錄 則繼續掃描if (file.isDirectory()) {findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes);} else {// 如果是java類文件 去掉后面的.class 只留下類名String className = file.getName().substring(0, file.getName().length() - 6);try {// 添加到集合中去// classes.add(Class.forName(packageName + '.' +// className));// 經過回復同學的提醒,這里用forName有一些不好,會觸發static方法,沒有使用classLoader的load干凈classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));} catch (ClassNotFoundException e) {// log.error("添加用戶自定義視圖類錯誤 找不到此類的.class文件");e.printStackTrace();}}}}
}

3、MyAnnotationConfigApplicationContext

package com.csdn.myspring;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class MyAnnotationConfigApplicationContext {private Map<String, Object> ioc = new HashMap<>();public MyAnnotationConfigApplicationContext() {}public MyAnnotationConfigApplicationContext(String pack) {//遍歷包,找到目標類(原材料)Set<BeanDefinition> beanDefinitions = findBeanDefinitions(pack);//根據原材料獲取beancreateObject(beanDefinitions);//自動裝配autowireObject(beanDefinitions);}public void autowireObject(Set<BeanDefinition> beanDefinitions) {Iterator<BeanDefinition> iterator = beanDefinitions.iterator();while (iterator.hasNext()) {BeanDefinition beanDefinition = iterator.next();Class clazz = beanDefinition.getBeanClass();Field[] declaredFields = clazz.getDeclaredFields();for (Field declaredField : declaredFields) {Autowired annotation = declaredField.getAnnotation(Autowired.class);if (annotation!=null) {Qualifier qualifier = declaredField.getAnnotation(Qualifier.class);if (qualifier!=null) {try {String beanName = qualifier.value();Object bean = getBean(beanName);String fieldName = declaredField.getName();String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);Method method = clazz.getMethod(methodName, declaredField.getType());Object object = getBean(beanDefinition.getBeanName());method.invoke(object, bean);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}Object object = getBean(beanDefinition.getBeanName());}}}}}public Set<BeanDefinition> findBeanDefinitions(String pack) {//獲取包下的所有類Set<Class<?>> classes = MyTools.getClasses(pack);Iterator<Class<?>> iterator = classes.iterator();Set<BeanDefinition> beanDefinitions = new HashSet<>();while (iterator.hasNext()) {//2、遍歷這些類,找到添加了注解的類Class<?> clazz = iterator.next();Component componentAnnotation = clazz.getAnnotation(Component.class);if (componentAnnotation != null) {//獲取Component注解的值String beanName = componentAnnotation.value();if ("".equals(beanName)) {//獲取類名首字母小寫String className = clazz.getName().replaceAll(clazz.getPackage().getName() + ".", "");beanName = className.substring(0, 1).toLowerCase() + className.substring(1);}//3、將這些類封裝成BeanDefinition,裝載到集合中beanDefinitions.add(new BeanDefinition(beanName, clazz));}}return beanDefinitions;}public void createObject(Set<BeanDefinition> beanDefinitions) {Iterator<BeanDefinition> iterator = beanDefinitions.iterator();while (iterator.hasNext()) {BeanDefinition beanDefinition = iterator.next();Class clazz = beanDefinition.getBeanClass();String beanName = beanDefinition.getBeanName();try {//創建對象Object object = clazz.getConstructor().newInstance();//完成屬性的賦值Field[] declaredFields = clazz.getDeclaredFields();for (Field declaredField : declaredFields) {Value valueAnnotation = declaredField.getAnnotation(Value.class);if (valueAnnotation!=null) {String value = valueAnnotation.value();String fieldName = declaredField.getName();String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);Method method = clazz.getMethod(methodName, declaredField.getType());//完成數據類型轉換Object val = null;switch (declaredField.getType().getName()) {case "java.lang.Integer" -> val=Integer.parseInt(value);case "java.lang.String"-> val = value;case "java.lang.Float"-> Float.parseFloat(value);}method.invoke(object, val);}}//存入緩存ioc.put(beanName, object);} catch (InstantiationException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (NoSuchMethodException e) {throw new RuntimeException(e);}}}public Object getBean(String beanName) {return ioc.get(beanName);}public static void main(String[] args) {MyAnnotationConfigApplicationContext myAnnotationConfigApplicationContext = new MyAnnotationConfigApplicationContext("com.csdn.myspring");Person bean = (Person) myAnnotationConfigApplicationContext.getBean("b");System.out.println(bean.getName());}
}

4、@Component

package com.csdn.myspring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {String value() default "";
}

5、@Value

package com.csdn.myspring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Value {String value();
}

6、@Autowired

package com.csdn.myspring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
}

7、@Qualifier

package com.csdn.myspring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Qualifier {String value();
}

@Qualifier是一個Spring框架的注解,用于標識一個Bean的特定實例。當有多個Bean實現了同一接口或類時,@Qualifier可以指定要使用的實例。

通常情況下,Spring框架根據類型來自動裝配依賴,但如果有多個 Bean 與依賴的類型匹配,則會產生歧義。這時就需要使用 @Qualifier 來指定具體匹配的 Bean。

例如:

public interface Animal {// ...
}@Component
@Qualifier("cat")
public class Cat implements Animal {// ...
}@Component
@Qualifier("dog")
public class Dog implements Animal {// ...
}@Service
public class AnimalService {@Autowired@Qualifier("cat")private Animal animal;// ...
}

在這個例子中,AnimalService 類需要注入一個 Animal 接口的實例,但有兩個實現類 Cat 和 Dog。使用 @Qualifier 標記 Cat 和 Dog 實例,然后在 AnimalService 中使用 @Autowired 和 @Qualifier("cat") 標記,就可以明確指定注入 Cat 實例了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/161761.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/161761.shtml
英文地址,請注明出處:http://en.pswp.cn/news/161761.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

@Scheduled注解 定時任務講解

用于在Java Spring框架中定時執行特定任務的注解 Scheduled&#xff0c;它能夠指定方法在特定時間間隔或特定時間點執行。默認參數是cron&#xff0c;cron參數被用來定義一個Cron表達式&#xff0c;它代表了任務執行的時間規則 參數如下 Cron 這是是一種時間表達式&#xff…

【應用程序啟動過程-三種加載控制器的方式-上午內容復習 Objective-C語言】

一、我們先來回憶一下,上午所有內容 1.首先呢,我們先說的是這個“應用程序啟動過程”, 應用程序啟動過程里面,有三方面內容 1)UIApplication對象介紹 2)AppDelegate對象介紹 3)應用程序啟動過程 現在不知道大家對這個應用程序啟動過程有印象嗎, 2.首先,這個UIAp…

MySQL數據庫時間計算的用法

今天給大家分享如何通過MySQL內置函數實現時間的轉換和計算&#xff0c;在工作當中&#xff0c;測試人員經常需要查詢數據庫表的日期時間&#xff0c;但發現開發人員存入數據庫表的形式都是時間戳形式&#xff0c;不利于測試人員查看&#xff0c;測試人員只能利用工具對時間戳進…

【 順序表經典算法—移除元素和合并兩個有序數組】

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 目錄 前言 經典算法OJ題1&#xff1a; 移除元素 解法一、逐個判斷 解法二、雙指針覆蓋 經典算法OJ題2&#xff1a; 合并兩個有序數組 OJ題分為兩個類型&#xff1a; 總結 前言…

MAX/MSP SDK學習07:list傳遞

實現自定義Obejct&#xff0c;要求將傳入的一組數據100后傳出。 #include "ext.h" #include "ext_obex.h" typedef struct _listTrans {t_object ob;void* outLet;t_atom* fArr;long listNum;} t_listTrans;void* listTrans_new(t_symbol* s, long arg…

14.Python 模塊

目錄 1. 使用模塊2. 使用包3. 常用模塊3.1 日期和時間3.2 偽隨機數3.3 摘要算法3.4 JSON 處理3.5 圖像處理 模塊是Python用來組織代碼的一種方法&#xff0c;包是Python用來組織模塊的一種方法。 1. 使用模塊 Python 把能夠相互包含&#xff0c;且有組織的代碼段稱為模塊&…

.NET面試題1

1.什么是C#&#xff1f; C#&#xff08;讀作"C sharp"&#xff09;是一種通用的、面向對象的編程語言&#xff0c;由Microsoft開發。它是一種靜態類型語言&#xff0c;支持強類型檢查和面向對象編程&#xff08;OOP&#xff09;的概念。C#主要用于開發Windows應用程序…

Bug等級劃分

Bug是指在程序或系統中存在的錯誤、缺陷或異常&#xff0c;是由于編碼錯誤、設計問題、邏輯錯誤或其他因素導致的。 常見的Bug分類方法 功能性Bug與軟件的功能有關&#xff0c;軟件無法正常工作、功能與需求不符或功能執行不正確。 用戶界面Bug與軟件的用戶界面有關&#xff…

Unity中Shader雙向反射分布函數BRDF

文章目錄 前言一、渲染方程二、什么是BxDF1、BSSRDF2、BRDF3、BTDF4、BSDF 三、迪士尼原則的BRDF四、迪士尼原則的BRDF的參數五、在Unity中看一下默認Shader的這些參數六、在這里記錄一下使用 Blender 和 SubstancePainter 的流程1、在Blender中導出模型為 .obj 格式2、在Subst…

Android WMS—— Surace管理 (二十)

WMS 負責創建 Surface 以及對 Surface 的擺放工作,之后將 Surface 提交給SurfaceFlinger 進行合并。在 App 層也創建了一個 Surface 對象,但是那個是空對象,用于 WMS 的填充。 一、Surface的創建 首先 APP 層在 ViewRootImpl 的 relayoutWindow() 方法中發起創建任務。 1、…

Go 實現網絡代理

使用 Go 語言開發網絡代理服務可以通過以下步驟完成。這里&#xff0c;我們將使用 golang.org/x/net/proxy 包來創建一個簡單的 SOCKS5 代理服務作為示例。 步驟 1. 安裝 golang.org/x/net/proxy 包 使用以下命令安裝 golang.org/x/net 包&#xff0c;該包包含 proxy 子包&am…

天軟特色因子看板 (2023.11 第12期)

該因子看板跟蹤天軟特色因子A05006(近一月單筆流入流出金額之比(%)&#xff0c;該因子為近一個月單筆流入流出金額之比(%)均值因子&#xff0c;用以刻畫在 市場日內分時成交中流入、流出成交金額的差異性特點&#xff0c;發掘市場主力資金的作用機制。 今日為該因子跟蹤第12期&…

expect腳本在自動化部署中的具體應用案例

#expect腳本在自動化部署中的具體應用 expect腳本是一個非常好的交互式應用腳本&#xff0c;在自動化部署中&#xff0c;可以使用這個腳本來實現全自動的自動化部署。下面是一些具體的應用案例。 場景一&#xff1a;自動安裝mysql 可以使用expect腳本來實現mysql自動安裝&…

Windows平臺Unity下實現camera場景推送RTMP|輕量級RTSP服務|實時錄像

技術背景 我們在對接Unity平臺camera場景采集的時候&#xff0c;除了常規的RTMP推送、錄像外&#xff0c;還有一些開發者&#xff0c;需要能實現輕量級RTSP服務&#xff0c;對外提供個拉流的RTSP URL。 目前我們在Windows平臺Unity下數據源可采集到以下部分&#xff1a; 采集…

@PostConstruct雖好,請勿亂用

1.問題說明 在日常的業務開發中&#xff0c;有時會利用PostConstruct在容器啟動時執行一些任務。例如&#xff1a; PostConstruct public void init(){System.out.println("service 初始化..............."); }一般情況這沒什么問題&#xff0c;但最近一個同事在做…

ui5使用echart

相關的代碼已經發布到github上。 展示下相關的實現功能 1、柱狀圖-1 2、柱狀圖-2 3.折線圖 4.餅狀圖 如何使用&#xff1a; 使用git clone項目到本地 git clone https://github.com/linhuang0405/com.joker.Zechart找到index.html。在vscode里右鍵選擇Open with Live Serve…

1

【任務 1】私有云服務搭建[10 分] 【題目 1】基礎環境配置[0.5 分] 【題目 2】Yum 源配置[0.5 分] 【題目 3】配置無秘鑰 ssh[0.5 分] 【題目 4】基礎安裝[0.5 分] 【題目 5】數據庫安裝與調優[0.5 分] 【題目 6】Keystone 服務安裝與使用[0.5 分] 【題目 7】Glance 安裝與使用…

BLE通用廣播包

文章目錄 1、藍牙廣播數據格式2、掃描響應數據 1、藍牙廣播數據格式 藍牙廣播包的最大長度是37個字節&#xff0c;其中設備地址占用了6個字節&#xff0c;只有31個字節是可用的。這31個可用的字節又按照一定的格式來組織&#xff0c;被分割為n個AD Structure。如下圖所示&…

npm命令

node -v --查看版本 npm install --安裝npm npm config get registry --查看npm當前鏡像 npm config set registry https://registry.npmmirror.com --設置淘寶鏡像 npm版本管理工具

VS Code 如何搭建C/C++環境

目錄 一、VS Code是什么&#xff1f; 二、VS Code下載和安裝 2.1下載 2.2安裝 2.3環境介紹 三、Vs Code配置C/C環境 3.1下載和配置MinGW-w64編譯器套件 3.1.1下載 3.1.2配置 一、VS Code是什么&#xff1f; 跨平臺&#xff0c;免費且開源的現代輕量級代碼編輯器 Vis…