Approximate idea
- Get the package path that the framework will load by reading the configuration file: Base-package, similar to the following in the Spring configuration file:
<base-package= "*"/>
- All classes under the Base-package path are loaded and stored in a set<class<?>> classset;
- Initialize the Bean container, traverse the Classset, get the instance of class by reflection, and save the mapping relationship between class and class instance, namely Map<class<?> object> Instancemap;
- Initialize the IOC, traverse the Bean container, find the Class with @Controller annotations, traverse its member variables, and if its member variables have @Inject annotations, get the corresponding Service class from Instancemap and set the bean instance's member variables;
Mind Mapping
Simple analog Ioc
Analog Controller class:
Packageorg.zhengbin.ioc.test;/*** Created by Zhengbinmac on 2017/4/10.*///Suppose this is a Controller class Public classTestController {//Suppose this is a Service to inject PrivateString Wordservice; //Suppose this is an Action method in which the service is invoked to implement the specific business logic Public voidtoout () {System.out.println ("Hello1" +Wordservice); } Public voidtoout (String str) {System.out.println ("Hello2" +str); }}
Analog IOC Injection Management:
Packageorg.zhengbin.ioc.test;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportJava.util.HashMap;ImportJava.util.Map;/*** Created by Zhengbinmac on 2017/4/10.*/ Public classTEMPIOC {//Bean container, which holds the mapping relationship between the Bean class and the bean instance Private StaticMap<class<?>, object> bean_map =NewHashmap<class<?>, object>(); Public Static voidMain (string[] args) {Try { //Loading class Instancesclass<?> CLA = Getclazz ("Org.zhengbin.ioc.test.TestController"); //deposit into Beanmap (i.e. put into Bean container)Object instance =newinstance (CLA); Bean_map.put (CLA, instance); //when needed (when initializing the entire WEB framework), get classes and class instances (that is, bean classes and bean instances) from Bean_mapObject Bean =Bean_map.get (CLA); //gets all the variables of the Reflection class, GetFields () is a variable that gets exposedfield[] Fields =Cla.getdeclaredfields (); for(Field field:fields) {//set member variable values for reflection class "instance"SetField (Bean, field, "Hello"); } //get all the methods of a Bean instanceMethod[] Methods =Cla.getdeclaredmethods (); for(Method method:methods) {//simulates whether the Action method needs to bring in parametersClass<?>[] Classes =method.getparametertypes (); if(Classes.length = = 0) { //Calling MethodsInvokeMethod (bean, method); } Else{InvokeMethod (bean, method,Hello); } } } Catch(Exception e) {Throw NewRuntimeException (e); } } /*** Load class *@paramfull path address of the PackageName class (package name. Class name) *@returnBean class*/ Private StaticClass<?>Getclazz (String packagename) {Class<?>CLS; Try{CLS=Class.forName (PackageName); } Catch(Exception e) {Throw NewRuntimeException (e); } returnCLS; } /*** Create an instance *@paramCLS Bean class *@returninstance of the Bean class*/ Private StaticObject newinstance (class<?>CLS) {Object instance; Try{instance=cls.newinstance (); } Catch(Exception e) {Throw NewRuntimeException (e); } returninstance; } /*** SET member variable values *@paramobj Bean instance *@paramfield member Variable *@paramassignment of the value member variable*/ Private Static voidSetField (Object obj, Field field, Object value) {Try { //A value of TRUE indicates that the reflected object should cancel the Java language access check when it is used. //a value of false indicates that the reflected object should implement a Java language access check. Field.setaccessible (true); Field.set (obj, value); } Catch(illegalaccessexception e) {e.printstacktrace (); } } /*** Call method *@paramobj Bean instance *@parammethod Methods (Action method in Controller) *@paramparameter of the args method *@returnmethod return value*/ Private Staticobject InvokeMethod (Object obj, Method method, Object ... args) {object result; Try{method.setaccessible (true); Result=method.invoke (obj, args); } Catch(Exception e) {Throw NewRuntimeException (e); } returnresult; }}
Output Result:
Hello1 Hello Hello2 Hello
A zero-write Java Web Framework--implementing IOC Dependency Injection