国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

簡單實現Spring的IOC原理詳解

2024-07-13 10:15:02
字體:
來源:轉載
供稿:網友

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

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

例如:

在Spring里面經常需要在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類代表的是需要從其他地方獲取天氣數據(數據庫或者服務器)

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

Weather類代表的是獲取到的天氣數據

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

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

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

首先是建立一個List模擬Spring的bean容器,即將已經裝初始化好的帶有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();		// 這里如果是普通調用會報空指針異常,而容器卻為其將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操作但是卻可以使用,這應該是最簡單的一個模擬Spring的IOC例子了,當然Spring的IOC容器比這個強大太多了,比如需要考慮線程安全,以及各種的細節問題

總結

以上就是本文關于簡單實現Spring的IOC原理詳解的全部內容,希望對大家有所幫助.


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜宾市| 无棣县| 玛沁县| 池州市| 崇仁县| 乌兰察布市| 和平区| 广汉市| 武强县| 成安县| 城固县| 安溪县| 铜川市| 鄂托克前旗| 淮安市| 金山区| 永靖县| 元谋县| 七台河市| 繁峙县| 垦利县| 四平市| 抚远县| 浪卡子县| 宜黄县| 宁津县| 拜城县| 东乡族自治县| 长垣县| 昌都县| 武冈市| 景洪市| 潜江市| 晴隆县| 达日县| 丰原市| 平度市| 田阳县| 社会| 利津县| 乡城县|