Spring笔记之三(Usage of property config)

本文介绍如何使用属性文件加载配置并结合工厂模式创建对象实例。通过定义Model和View接口及其实现类,利用属性文件指定具体实现类,并在工厂类中通过反射创建这些实例。

本文内容演示属性文件用法的简单例子。
首先定义模型接口Model,视图接口View,以及它们实现类ImpModel,ImpView.
Model中定义方法getString();View中定义Model实例为其属性,在实现类ImpView中实现方法
disPlay(),其方法体为Model实例的getString()方法,并将其打印出来。

定义属性文件
msf.properties
disPalyer.class
= ImpView
provider.class
= ImpModel


编写工厂类

TestFactory.java
import  java.util.Properties;
public   class  TestFactory{
    
private   static  TestFactory instance  =   null ;
    
private  Properties props  =   null ;
    
private  View disPalyer  =   null ;
    
private  Model provider  =   null ;

    
static  {
        instance 
=   new  TestFactory();
    }

    
public   static  TestFactory getInstance() {
        
return  instance;
    }

    
public  View getView() {
        
return  disPalyer;
    }

    
public  Model getModel() {
        
return  provider;
    }

    
private  TestFactory() {
        props 
=   new  Properties();

        
try  {
            
// 加载属性文件
            props.load( TestFactory. class .getResource( " msf.properties " ).openStream()); 

            
// 获取属性值
            String rendererClass  =  props.getProperty( " renderer.class " );
            String providerClass 
=  props.getProperty( " provider.class " );

            
// 产生类实例对象
            renderer  =  (View) Class.forName(rendererClass).newInstance();
            provider 
=  (Model) Class.forName(providerClass).newInstance();
        } 
catch  (Exception ex) {
            ex.printStackTrace();
        }
    }
}


最后编写测试方法

1  public static void main(String[] args) {
2             View mr = TestFactory.getInstance().getView();
3             Model mp = TestFactory.getInstance().getModel();
4             mr.setModel(mp);
5             mr.disPlay();
6         }


综上,对属性文件的加载主要语句
props.load( TestFactory.class.getResource("msf.properties").openStream());
并分别通过newInstance()产生实例对象,本例中使用了设计模式中的工厂模式以及单例实例模式,
因此具有对设计模式进一步了解的意义。


注:另一用法

 1 public static void main(String[] args) throws Exception {
 2 
 3         // get the bean factory
 4         BeanFactory factory = getBeanFactory();
 5 
 6         View mr = (View) factory.getBean("view");
 7         mr.render();
 8     }
 9 
10     private static BeanFactory getBeanFactory() throws Exception {
11         // get the bean factory
12         DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
13 
14         // create a definition reader
15         PropertiesBeanDefinitionReader rdr = new PropertiesBeanDefinitionReader(
16                 factory);
17 
18         // load the configuration options
19         Properties props = new Properties();
20         props.load(TestFactory.class.getResource("msf.properties").openStream());
21 
22         rdr.registerBeanDefinitions(props);
23 
24         return factory;
25     }
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值