溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

簡單實現(xiàn)Spring的IOC原理詳解

發(fā)布時間:2020-10-02 03:44:22 來源:腳本之家 閱讀:169 作者:Somersames 欄目:編程語言

控制反轉(zhuǎn)(InversionofControl,縮寫為IoC)

簡單來說就是當自己需要一個對象的時候不需要自己手動去new一個,而是由其他容器來幫你提供;Spring里面就是IOC容器。

例如:

在Spring里面經(jīng)常需要在Service這個裝配一個Dao,一般是使用@Autowired注解:類似如下

 public Class ServiceImpl{
  @Autowired
   Dao dao;
   
  public void getData(){
    dao.getData();
  }

在這里未初始化Dao直接使用是會報出空指針異常的,那么在Spring里面的做法就是通過反射來將需要的類幫你加載進來。

下面是一個例子模擬了Spring的DI和IOC

首先寫兩個注解模擬Spring的注解:

Entity注解代表的是Spring的@Service
@Target(ElementType.TYPE) // 類
@Retention(RetentionPolicy.RUNTIME)
public @interface Entity {
}

代表的是Spring里面的@Autowrid
@Target(ElementType.FIELD) //描述方法的
@Retention(RetentionPolicy.RUNTIME) // 僅運行時保留
public @interface Resources {
}

當注解建立完成之后再建立兩個類:
Rain類代表的是需要從其他地方獲取天氣數(shù)據(jù)(數(shù)據(jù)庫或者服務(wù)器

public class Rain {
  public void rain(){
    System.out.println("正在下雨"); // 為了方便直接寫了
  }
}

Weather類代表的是獲取到的天氣數(shù)據(jù)

@Entity
public class Weather {
  @Resources
  Rain rain; // 這里在后面通過反射直接注入rain
  public void weather_rain() {
    rain.rain();
  }

下面是通過反射來直接注入:

首先遍歷指定的包名:這一步先省略,

首先是建立一個List模擬Spring的bean容器,即將已經(jīng)裝初始化好的帶有Entity注解的類全部初始化

public class Weather_reflec {
	List<Object> objectList ;
	// 模擬Spring容器
	public Weather_reflec() {
		objectList= new ArrayList<Object>();
	}
	// 在這里其實最好的做法是先找出帶有注解的類,判斷帶有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保存進容器
				}
			}
		}
	}
	public List<Object> returnList(){
		return objectList;
		//返回容器方便以后使用
	}

最后也就是模擬Controller里面直接使用的

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)用會報空指針異常,而容器卻為其將rain這個變量賦值了,所以可以正常輸出
	}
	/*
  模擬Spring啟動過程,這一步其實可以單獨寫一個類,這一步是容器該做的,而我們并不需要去管
   */
	public Object springDo() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
		Weather_reflec weather_reflec =new Weather_reflec();
		// 啟動的時候就需要加載的
		Weather weather =new Weather();
		//掃描類注解后new操作然后進行下一步
		weather_reflec.get_ref(weather);
		// 將其類里面的變量進行new操作并放入容器
		Object object =weather_reflec.returnList().get(0);
		return object;
	}
	運行后輸出:正在下雨

在WeatherPrediction里面并沒有對Rain進行一個new操作但是卻可以使用,這應(yīng)該是最簡單的一個模擬Spring的IOC例子了,當然Spring的IOC容器比這個強大太多了,比如需要考慮線程安全,以及各種的細節(jié)問題

總結(jié)

以上就是本文關(guān)于簡單實現(xiàn)Spring的IOC原理詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Spring IOC原理詳解

簡單理解Spring之IOC和AOP及代碼示例

如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI