[Java] 设计模式之工厂系列 03- spring.properties 的键值读取 / Spring3.0 读取 比较
来源:互联网 发布:水果软件12教程 编辑:程序博客网 时间:2024/05/17 23:04
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 {@Overridepublic void run() {System.out.println("小火车呜呜呜");}}spring.properties 这里用配置文件,所以不需要更改代码,只需要更改, Car - Train 什么的就好!
VehicleType=com.bjsxt.spring.factory.CarTest
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();}}
0 0
- [Java] 设计模式之工厂系列 03- spring.properties 的键值读取 / Spring3.0 读取 比较
- [Java] 设计模式之工厂系列 04 (自定义模拟 spring 读取xml文件 beanFactory)
- java实现读取.properties的键值
- [Java 15 反射机制 ] 工厂模式与 properties 读取配置文件
- java设计模式系列之工厂模式
- 通过Spring读取properties配置文件的信息 Spring 读取properties
- spring读取properties的方法
- spring读取properties的方法
- Java之properties文件读取
- java设计模式之工厂系列
- JAVA设计模式之工厂系列
- xml中直接读取properties键值的解决方式
- osgi系列之— .properties文件读取
- 天易39----java与spring读取properties文件的方式
- java用spring实现配置文件properties的读取
- Java系列-如何读取.properties属性文件
- Java系列-读取properties文件连接数据库
- java读取properties的方法
- 如果你在做安利或者认识做安利的人
- 书籍的力量
- 1024. Palindromic Number (25)
- 平衡二叉树 实例
- 雷锋网2012应用下载排行榜
- [Java] 设计模式之工厂系列 03- spring.properties 的键值读取 / Spring3.0 读取 比较
- 解决CentOS 与 Ubuntu 中 Android ADT Failed to start emulator无法启动AVD的问题
- 碰到的word文档提示找不到package解决办法
- 体验新理财方式:余额宝与微信理财
- /bin/sh: autoreconf: command not found (centos 6.5 install qemu from source code)
- android:textAlignment 参数解释
- 文件I/O操作之open 的O_APPEND参数
- Android中Bitmap、Drawable、byte[]转换
- js正则表达式把不符合条件的字符替换处理(总结)