zoukankan      html  css  js  c++  java
  • [Java] 设计模式之工厂系列 03- spring.properties 的键值读取 / Spring3.0 读取 比较

    Moveable
    package com.bjsxt.spring.factory;
    
    public interface Moveable {
    	void run();
    }
    
    Car
    package com.bjsxt.spring.factory;
    
    public class Car implements Moveable {
    
    	public void run() {
    		System.out.println("冒着烟奔跑中car.......");
    	}
    }
    
    Train
    package com.bjsxt.spring.factory;
    
    public class Train implements Moveable {
    
    	@Override
    	public void run() {
    		System.out.println("小火车呜呜呜");
    	}
    
    }
    
    spring.properties    这里用配置文件,所以不需要更改代码,只需要更改, Car - Train 什么的就好!
    VehicleType=com.bjsxt.spring.factory.Car
    Test
    package com.bjsxt.spring.factory;
    
    import java.io.IOException;
    import java.util.Properties;
    
    public class Test {
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws Exception {
    		Properties props = new Properties();
    		props.load(Test.class.getClassLoader().getResourceAsStream("com/bjsxt/spring/factory/spring.properties"));
    		String vehicleTypeName = props.getProperty("VehicleType");
    		System.out.println(vehicleTypeName);
    		Object o = Class.forName(vehicleTypeName).newInstance();
    		Moveable m = (Moveable)o;
    		m.run();
    	}
    }
    

    ---------Spring3.0 读取 比较---------------------- 将所需 jar包 加入 build path----

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
      <bean id="v" class="com.bjsxt.spring.factory.Car">  
      </bean>
      <!--  v=com.bjsxt.spring.factory.Car -->
    
    
    </beans>
    Test
    package com.bjsxt.spring.factory;
    
    import java.io.IOException;
    import java.util.Properties;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws Exception {
    		BeanFactory f = new ClassPathXmlApplicationContext("applicationContext.xml");
    		Object o = f.getBean("v");
    		Moveable m = (Moveable)o;
    		m.run();
    	}
    }
    





  • 相关阅读:
    SQL 使用identity(int,1,1)来产生行号。
    SQL DateName\DatePart 返回表示指定date的指定datepart的字符串
    让我们受用一生的好习惯
    SCRUM软件开发过程(转)
    计算机英语词汇
    oral English英语绕口令(转)
    Setup相关经验总结
    与老外吵架之必会109句
    BAT批处理文件语法(转)
    SQL Server 2005之PIVOT/UNPIVOT行列转换(转)
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786548.html
Copyright © 2011-2022 走看看