java IoC,javaioc
IoC,控制反轉,是spring的核心,通俗點講就是我們不必再自己去用new建立對象了,通過l配置將類注入到IoC容器中,在啟動時,IoC容器去幫我們建立對象,並管理其依賴關係,這種實現方式叫做DI,依賴注入。為什麼我們要用IoC去幫我們管理對象呢,因為通常一個商務邏輯都是由多個對象合作完成工作的,如果自己管理的話比較,就要在一個方法中調用多個對象,不好管理,重用性低。
//使用原廠模式加設定檔的方式模範了一個簡易IoC的工作原理 //1.BeanFactory.java bean工廠,建立對象 //2.PropertiesReader.java 讀取設定檔 //3.普通bean類 //4.測試類別
1 package myIoC; 2 /** 3 * 1.讀取設定檔介面,有兩個實作類別,一個用類載入路徑讀取設定檔,第二個用檔案路徑讀取設定檔 4 * 2.提供BeanFactory 方法 5 * @author LH-PC 6 * 7 */ 8 public interface ApplicationContext { 9 10 /**11 * beanFactory 方法12 * @param name13 * @return14 */15 Object getBean(String name);16 Object getBean(String name, String className);17 18 }
1 package myIoC; 2 3 import java.util.Date; 4 import java.util.Map; 5 6 /** 7 * ApplicationContext實作類別之一,類載入讀取設定檔 8 * @author LH-PC 9 *10 */11 public class ClassPathXmlApplicationContext implements ApplicationContext{12 13 private String propertiesName;14 15 /**16 * 構造方法用於載入設定檔17 * @param xml18 */19 public ClassPathXmlApplicationContext(String propertiesName){20 //初始化屬性21 this.propertiesName = propertiesName;22 }23 24 /**25 * 通過類載入方式的Factory 方法26 */27 public Object getBean(String name) {28 //讀取設定檔29 PropertiesReader propertiesReader = new PropertiesReader(propertiesName);30 Object object = null;31 //建立對象32 Map<String, String> map = propertiesReader.getProperties();33 try {34 System.err.println(new Date() +": " + "BeanFactory開始生產對象...");35 object = Class.forName(map.get(name)).newInstance();36 System.err.println(new Date() +": " + "生產對象完畢");37 } catch (InstantiationException e) {38 System.err.println(new Date() +": " + "建立對象異常");39 e.printStackTrace();40 } catch (IllegalAccessException e) {41 System.err.println(new Date() +": " + "IllegalAccessException異常");42 e.printStackTrace();43 } catch (ClassNotFoundException e) {44 System.err.println(new Date() +": " + "類載入異常");45 e.printStackTrace();46 }47 48 return object;49 }50 51 public Object getBean(String name, String className) {52 // TODO Auto-generated method stub53 return null;54 }55 56 }
1 package myIoC; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Date; 6 import java.util.Enumeration; 7 import java.util.HashMap; 8 import java.util.Map; 9 import java.util.Properties;10 11 //讀取properties檔案類12 public class PropertiesReader {13 14 private Properties pro = null;15 private InputStream in = null;16 private String propertiesName = null;17 18 public PropertiesReader(String propertiesName){19 this.propertiesName = propertiesName;20 }21 22 @SuppressWarnings("rawtypes")23 public Map<String,String> getProperties(){24 Map<String, String> map = new HashMap<String, String>();25 pro = new Properties(); //建立properties對象26 try {27 System.err.println(new Date() +": " + "開始讀取" + propertiesName + "...");28 in = PropertiesReader.class.getClassLoader().getResourceAsStream(propertiesName); //通過反射機制讀取設定檔29 if(in == null){30 System.err.println(new Date() +": " + "讀取流為空白");31 }else{32 //開始負載檔案內容33 pro.load(in);34 System.err.println(new Date() +": " + propertiesName + "讀取成功");35 }36 Enumeration en = pro.propertyNames(); //迭代器遍曆pro37 while(en.hasMoreElements()){38 String key = (String) en.nextElement(); //遍曆key39 String value = pro.getProperty(key); //根據key取出value,放進map40 map.put(key,value);41 }42 } catch (IOException e) {43 System.err.println(new Date() +": " + "io異常");44 e.printStackTrace();45 }46 finally{47 if(in != null){48 try {49 in.close();//關閉讀取流50 System.err.println(new Date() +": " + "讀取流關閉成功");51 } catch (IOException e) {52 System.err.println(new Date() +": " + "讀取流關閉失敗");53 e.printStackTrace();54 } 55 }56 }57 return map;58 }59 60 public static void main(String[] args) {61 PropertiesReader propertiesReader = new PropertiesReader("myIoc/applicationContext.properties");62 Map<String, String> map = propertiesReader.getProperties();63 System.out.println("ok");64 }65 66 }
student=myIoC.Studentbean2=test.BeansImpl2bean3=test.BeansImpl3
1 package myIoC; 2 3 /** 4 * MyIoC測試類別 5 * @author LH-PC 6 */ 7 public class IoCTest { 8 public static void main(String[] args) { 9 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("myIoc/applicationContext.properties");10 Student student = (Student) applicationContext.getBean("student");11 student.setSid("15301");12 student.setSname("海");13 System.out.println(student.getSid());14 System.out.println(student.getSname());15 16 }17 18 }19 20 /**21 * 測試實體類22 * @author LH-PC23 *24 */25 class Student {26 private String sname;27 private String sid;28 29 public String getSname() {30 return sname;31 }32 33 public void setSname(String sname) {34 this.sname = sname;35 }36 37 public String getSid() {38 return sid;39 }40 41 public void setSid(String sid) {42 this.sid = sid;43 }44 45 }