java IoC。本站提示廣大學習愛好者:(java IoC)文章只能為提供參考,不一定能成為您想要的結果。以下是java IoC正文
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.提供Bean工廠方法
5 * @author LH-PC
6 *
7 */
8 public interface ApplicationContext {
9
10 /**
11 * bean工廠方法
12 * @param name
13 * @return
14 */
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 xml
18 */
19 public ClassPathXmlApplicationContext(String propertiesName){
20 //初始化屬性
21 this.propertiesName = propertiesName;
22 }
23
24 /**
25 * 通過類加載方式的工廠方法
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 stub
53 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(); //迭代器遍歷pro
37 while(en.hasMoreElements()){
38 String key = (String) en.nextElement(); //遍歷key
39 String value = pro.getProperty(key); //根據key取出value,放進map
40 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.Student bean2=test.BeansImpl2 bean3=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-PC
23 *
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 }