Spring IOC容器的簡單實現

2021-10-08 02:30:44 字數 2650 閱讀 5108

inverse of control(控制反轉) 將原本在程式中手動建立物件的控制權,交由spring框架來管理 若要使用某個物件,只需要從 spring 容器中獲取需要使用的物件,不關心物件的建立過程,也就是把建立物件的控制權反轉給了spring框架。

github只有src下的檔案和pom.xml檔案,**帶有詳細的注釋,請自取;

github用不了的可以點這裡

專案結構目錄如下:

annotation實現一些需要的註解

@retention(retentionpolicy.runtime)//執行時註解

@target(elementtype.field)//作用在字段上

@documented

public @inte***ce autowire

@retention(retentionpolicy.runtime)//執行時註解

@target(elementtype.type)//作用在類上

@documented

public @inte***ce component

@retention(retentionpolicy.runtime)//執行時註解

@target(elementtype.type)//作用在類上

@documented

public @inte***ce controller

@retention(retentionpolicy.runtime)//執行時註解

@target(elementtype.parameter)//作用在方法引數上面

@documented

public @inte***ce requestparam

@retention(retentionpolicy.runtime)//執行時註解

@target()//作用在類和方法,參考springmvc

@documented

string value() default "";

}

object getbean(string id);

t getbean(string id, classclazz);

mapgetbeans();

}public inte***ce beanregister

一些預設的工廠實現

public class beancreater

public void create(listbds)

}public void docreate(beandefinition bd)

}public class beandefinition

public object getinstance()

catch(exception e)

return null;

}//region getter && setter

public string getid()

public void setid(string id)

public class getclazz()

public void setclazz(class clazz)

//endregion

}public class beandefinitiongenerator

listlist = new arraylist();

for(string id:ids)

return list;

}catch(classnotfoundexception e)

return null;

}private static string generateids(class clazz);

}else if(clazz.isannotationpresent(component.class));

}else

return ids;}}

return ids;

}}public class beandefinitionparser

public void parse(properties properties)

public void doregister(string packagename)

else}}

}public class populator

field fields = entry.getvalue().getclass().getdeclaredfields();

for (field field : fields) catch (illegalacces***ception e) }}

}}

最後的測試類

@controller

public class mycontroller

public static void main(string args)

}@component("myservice")

public class myservice

}

spring原始碼--ioc容器實現(6)--bean依賴注入

自己實現spring(一) —— ioc容器實現

Spring IoC容器的簡單介紹

ioc 是 inversion of control 的縮寫,就是控制反轉的意思,他是指在開發過程中,物件的建立不再由呼叫者處理,而是交給 spring 容器來建立。spring 容器會控制程式之間的關係,而是不通過 直接控制,所以控制權有程式 轉移給了 spring容器,控制權發生反轉。簡單來說就...

spring IOC容器的擴充套件

在此之前已經完成了ioc對 xml的解析和例項化工作,接下來需要分析 spring 的高階版本對 ioc容器的功能擴充套件 分析如下 synchronized this.startupshutdownmonitor 接下來,即開始對上面的步奏進行一一的講解 preparerefresh 準備重新整理...

springIOC容器詳解

springioc容器又被搞了,我tm今天要搞定這個東西 我目前看過最好的講解是這篇作者的 spring的ioc容器在實現控制反轉和依賴注入的過程中,可以劃分為兩個階段 這兩個階段中,ioc容器分別作了以下這些事情 2 ioc容器及ioc容器如何獲取物件間的依賴關係 spring中提供了兩種ioc容...