Spring(1)-IOC-反射

Spring IOC(控制反转).(此文部分代码和配置文件来源于网络)
1.什么叫反射
2.常用方法
3.反射的代码:
  3.1主类
  3.2配置文件
     如果想要使用这种模式,那么你得搭建一个Spring的运行环境
   3.2.1依赖文件
   3.2.2spring.xml 
(非new的创建对象)

      a)无参构造函数创建对象
      b)有参构造函数创建对象
      c)索引构造函数创建对象
      d) 多例,非单实例
  3.3调用


1.什么叫反射
  
程序运行时进行自我检查. ​ 动态地对对象进行编译。 ​
   我的理解,相对于C++中的一个基类,通过配置文件,动态的生成子类。
   最重要不用new。
2.常用方法
 

getName() 获得类的完整名字
getPackage()获取此类所属的包
getSuperclass()获得此类的父类对应的Class对象
getField(String name)获得类的指定属性
getMethods()获得类的public类型的方法
getMethod (String name,Class [] args)获得类的指定方法


3.反射的代码:
  3.1主类
   

interface Worker 
{
    public abstract void work();
}

class Coder implements Worker {
    public void work() {
        System.out.println("I am coding.");
    }
}

class Leader implements Worker {
    public void work() {
        System.out.println("I am talking.");
    }
}

// 操作属性文件类
class init{
    public static Properties getPro() throws FileNotFoundException, IOException{
        Properties pro=new Properties();
        File f=new File("worker.properties");
        if(f.exists()){
            pro.load(new FileInputStream(f));
        }else{
            pro.setProperty("coder", "worker.Coder");
            pro.setProperty("leader", "worker.Leader");
            pro.store(new FileOutputStream(f), "WORKER CLASS");
        }
        return pro;
    }
}

// 构造工厂
class Factory{
    public static Worker getInstance(String ClassName) {
        Worker worker = null;
        try{
            worker = (Worker)Class.forName(ClassName).newInstance();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return worker;
    }
}

// 测试类
class Test {
    public static void main(String args[]){
        Properties pro=init.getPro();
        Worker worker = Factory.getInstance(pro.getProperty("coder")); // 
        if(worker!=null) {
            worker.work();
        }
        
    }
}


  3.2配置文件
   xxx.properties的格式命名,此处为worker.properies

coder=worker.Coder
leader=worker.Leader


  如果想要使用这种模式,那么你得搭建一个Spring的运行环境
   3.2.1依赖文件

 


  4.0.0

  com.zxl
  MyProject
  0.0.1-SNAPSHOT
  jar

  MyProject
  http://maven.apache.org


        UTF-8
        4.3.0.RELEASE
    
    
        
            junit
            junit
            test
            4.10
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
        
            cglib
            cglib
            3.2.4
        
    



   3.2.2spring.xml 
      a)无参



         
     


      b)有参

 
     
        
        
     


      c)索引
 


        
        

  d)  多例 非单例 scope="prototype", 一个bean对应多个实例对象。
   





  3.3调用
 

public class Test {
    public static void main(String[] args) {
        //1.加载spring.xml配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //2.通过id值获取对象
        Worker worker1 = (Worker) applicationContext.getBean("worker1");
        System.out.println(worker1);
    }
}



 

你可能感兴趣的:(JAVA)