spring 下得到properties中的配置文件

来源:互联网 发布:想买摔炮在淘宝怎么搜 编辑:程序博客网 时间:2024/06/05 19:56
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

public class SystemConfig {
private static Properties props = new Properties(); 

static{
try {
props.load(getLocation().getInputStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static String getValue(String key){
return props.getProperty(key);
}
    public static void updateProperties(String key,String value) {    
         props.setProperty(key, value); 
    }
    
    /*
     * getServerName
     * 获取当前服务器运行环境的名称,用来标示是在运行环境中的哪台服务器
     */
    public static String getServerName(){
   
    String serverName = getValue("server_name");
    if(null!=serverName)
    return serverName;
    else
    return "Unknown Server";
    }
    
public static Resource getLocation() {
Resource location =null;
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{});
if(null==location){
location = context.getResource("classpath:PhotoConfig.properties");
}
return location;
}

public static Resource getResource(String fileName) {
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{});
return context.getResource("classpath:"+fileName);
}

public static void main(String[] args) {
System.out.println(SystemConfig.getValue("server_name"));
}
}
0 0