zoukankan      html  css  js  c++  java
  • 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 }
     
  • 相关阅读:
    TIOBE 11月指数:C语言居首,稳居宝座,Python直逼第二!
    毕业即失业?大学生如何分配学习时间比例,拥有完整计算机知识体系?
    用微信表情翻译表白,程序员的小浪漫,赶紧Get起来!
    趣文分享:C 语言和 C++、C# 的区别在什么地方?
    C++基础知识篇:C++ 基本语法
    盘点那些争议最大的编程观点,你是什么看法呢?
    小米死磕硬核技术,将扩招5000名工程师,多个领域会使用到C++
    Gentle guide on how YOLO Object Localization works with Keras (Part 2)
    HUMBLE YOLO IMPLEMENTATION IN KERAS
    Training and Detecting Objects with YOLO3
  • 原文地址:https://www.cnblogs.com/liaohai/p/6420044.html
Copyright © 2011-2022 走看看