Spring加载配置信息

constant.properties
xx=444
yyy=8888
xxxxx=999999


<bean id="constant" class="com.common.Constant" init-method="init" scope="singleton"></bean>



package com.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Constant {
	private static Properties pros = new Properties();
	
	public void init() {
		InputStream inputStream = this.getClass().getClassLoader()
		.getResourceAsStream("constant.properties");
		try {
			pros.load(inputStream);
//			Iterator<Object> i = p.keySet().iterator();
//			while(i.hasNext()){
//				Object key = i.next();
////				constantMap.put((String) key, p.getProperty((String)key));
//				System.out.println("****"+key+"="+p.getProperty((String)key));
//			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}
	 private Constant(){};
	 
	 public static final String getProperties(String key){
	  return Constant.pros.getProperty(key);
	 }
	}




public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		        new String[] {"spring/applicationContext.xml"});
		TTinterface  tt=(TTinterface)context.getBean("t");
		Constant  constant = (Constant) context.getBean("constant");
		System.out.println(Constant.getProperties("xx"));
		System.out.println(Constant.getProperties("xxxxx"));

	}

你可能感兴趣的:(spring)