控制反轉(zhuǎn)(InversionofControl,縮寫為IoC)
簡(jiǎn)單來(lái)說(shuō)就是當(dāng)自己需要一個(gè)對(duì)象的時(shí)候不需要自己手動(dòng)去new一個(gè),而是由其他容器來(lái)幫你提供;Spring里面就是IOC容器。
例如:
在Spring里面經(jīng)常需要在Service這個(gè)裝配一個(gè)Dao,一般是使用@Autowired注解:類似如下
1
2
3
4
5
6
7
|
public Class ServiceImpl{ @Autowired Dao dao; public void getData(){ dao.getData(); } |
在這里未初始化Dao直接使用是會(huì)報(bào)出空指針異常的,那么在Spring里面的做法就是通過(guò)反射來(lái)將需要的類幫你加載進(jìn)來(lái)。
下面是一個(gè)例子模擬了Spring的DI和IOC
首先寫兩個(gè)注解模擬Spring的注解:
1
2
3
4
5
6
7
8
9
10
11
|
Entity注解代表的是Spring的 @Service @Target (ElementType.TYPE) // 類 @Retention (RetentionPolicy.RUNTIME) public @interface Entity { } 代表的是Spring里面的 @Autowrid @Target (ElementType.FIELD) //描述方法的 @Retention (RetentionPolicy.RUNTIME) // 僅運(yùn)行時(shí)保留 public @interface Resources { } |
當(dāng)注解建立完成之后再建立兩個(gè)類:
Rain類代表的是需要從其他地方獲取天氣數(shù)據(jù)(數(shù)據(jù)庫(kù)或者服務(wù)器)
1
2
3
4
5
|
public class Rain { public void rain(){ System.out.println( "正在下雨" ); // 為了方便直接寫了 } } |
Weather類代表的是獲取到的天氣數(shù)據(jù)
1
2
3
4
5
6
7
|
@Entity public class Weather { @Resources Rain rain; // 這里在后面通過(guò)反射直接注入rain public void weather_rain() { rain.rain(); } |
下面是通過(guò)反射來(lái)直接注入:
首先遍歷指定的包名:這一步先省略,
首先是建立一個(gè)List模擬Spring的bean容器,即將已經(jīng)裝初始化好的帶有Entity注解的類全部初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class Weather_reflec { List<Object> objectList ; // 模擬Spring容器 public Weather_reflec() { objectList= new ArrayList<Object>(); } // 在這里其實(shí)最好的做法是先找出帶有注解的類,判斷帶有Entity注解再傳入。但是為了方便直接省略了 public void get_ref(Object object) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { Class<?> clazz =object.getClass(); if (clazz.isAnnotationPresent(Entity. class )){ //判斷是否帶有Entity注解 Field[] fields =clazz.getDeclaredFields(); //獲取其變量 for (Field field :fields){ if (field.isAnnotationPresent(Resources. class )){ //判斷是否需要注入 Class<?> rainClass=Class.forName(field.getType().getName(), false ,Thread.currentThread().getContextClassLoader()); // 這里先將Rain類加載 field.set(object,rainClass.newInstance()); //賦給rain objectList.add(object); //最后將已將賦值后的Weather保存進(jìn)容器 } } } } public List<Object> returnList(){ return objectList; //返回容器方便以后使用 } |
最后也就是模擬Controller里面直接使用的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class WeatherPrediction { public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { WeatherPrediction weatherPrediction = new WeatherPrediction(); Weather weather =(Weather)weatherPrediction.springDo(); weather.weather_rain(); // 這里如果是普通調(diào)用會(huì)報(bào)空指針異常,而容器卻為其將rain這個(gè)變量賦值了,所以可以正常輸出 } /* 模擬Spring啟動(dòng)過(guò)程,這一步其實(shí)可以單獨(dú)寫一個(gè)類,這一步是容器該做的,而我們并不需要去管 */ public Object springDo() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { Weather_reflec weather_reflec = new Weather_reflec(); // 啟動(dòng)的時(shí)候就需要加載的 Weather weather = new Weather(); //掃描類注解后new操作然后進(jìn)行下一步 weather_reflec.get_ref(weather); // 將其類里面的變量進(jìn)行new操作并放入容器 Object object =weather_reflec.returnList().get( 0 ); return object; } 運(yùn)行后輸出:正在下雨 |
在WeatherPrediction里面并沒有對(duì)Rain進(jìn)行一個(gè)new操作但是卻可以使用,這應(yīng)該是最簡(jiǎn)單的一個(gè)模擬Spring的IOC例子了,當(dāng)然Spring的IOC容器比這個(gè)強(qiáng)大太多了,比如需要考慮線程安全,以及各種的細(xì)節(jié)問(wèn)題
總結(jié)
以上就是本文關(guān)于簡(jiǎn)單實(shí)現(xiàn)Spring的IOC原理詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
原文鏈接:https://segmentfault.com/a/1190000009139271