java中读取配置文件方法二

在项目启动时加载


package com.founder.ids.listener;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.core.io.ClassPathResource;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.founder.ids.cache.CacheManager;
import com.founder.ids.common.WebProperties;
import com.founder.ids.entity.Organization;
import com.founder.ids.service.IHealthOfficerService;
import com.founder.ids.service.IOrganizationService;

public class PropertiesInitListener implements ServletContextListener {

private static final String WEBSERVICE_CONFIG_FILE = "config/common-web.properties";

//@Resource(name = "organizationService")
IOrganizationService organizationService;
IHealthOfficerService doctorService;

@Override
public void contextDestroyed(ServletContextEvent event) {
// System.out.println("===hah2====");
}

@Override
public void contextInitialized(ServletContextEvent event) {
organizationService = this.getOrganizationService(event.getServletContext());
List<Organization> list = organizationService.getOrganizations();
for(Organization org:list){
CacheManager.orgWebServiceUrlMap.put(org.getId().toString(), org.getWebserviceUrl());
}
ClassPathResource cr = new ClassPathResource(WEBSERVICE_CONFIG_FILE);
try {
WebProperties.getInstance();
WebProperties.getProperties().load(cr.getInputStream());
} catch (IOException ex) {
throw new RuntimeException("Cannot find Configuration " + PropertiesInitListener.WEBSERVICE_CONFIG_FILE);
}
}

protected IOrganizationService getOrganizationService(ServletContext servletContext) {
return (IOrganizationService) WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("organizationService");
}
}


代码实现
package com.founder.ids.common;

import java.util.Properties;

import com.founder.fasf.util.ObjectUtil;

public class WebProperties {
private static final WebProperties instance = new WebProperties();

private static Properties properties;

public static Properties getProperties() {
return properties;
}

public static void setProperties(Properties properties) {
WebProperties.properties = properties;
}

private WebProperties(){

}

public static WebProperties getInstance(){
if(properties == null){
properties = new Properties();
}
return instance;
}

/**
* 根据Key获取msg
* @param msgKey
* @param ...args
*
* */
public static String getMsg(String msgKey,String ...args){
String msg = properties.getProperty(msgKey);

if(msg == null){
return "";
}

if(args == null || args.length == 0){
return msg;
}
for(int i = 0; i < args.length; i++){
String reg = "{" + i + "}";
String rep = args[i];
if(ObjectUtil.isNotEmpty(rep)) {
msg = msg.replace(reg, rep);
}
}
return msg;
}
}

如何调用

package com.founder.ids.common;

import com.founder.fasf.util.ObjectUtil;


public class SiteNoticeUtil {
/**
* 根据Key获取msg
* @param msgKey
* @param ...args
*
* */
public static String getMsg(String msgKey,String ...args){
String msg = WebProperties.getMsg(msgKey);

if(msg == null){
return "";
}

if(args == null || args.length == 0){
return msg;
}
for(int i = 0; i < args.length; i++){
String reg = "{" + i + "}";
String rep = args[i];
if(ObjectUtil.isNotEmpty(rep)) {
msg = msg.replace(reg, rep);
}
}
return msg;
}
}
SiteNoticeUtil.getMsg(
"sms.reserve.reg.cancel",
DateUtil.getDateTime("yyyy/MM/dd", reserveRegister.getReserveDate()) + " "
+ cv.getName(), organizationDao.get(reserveRegister.getHospitalId())
.getName(), reserveRegister.getDoctor().getName())


文件common-web.properties

######################短信内容#############
sms.reserve.reg.create=您预约的{0}{1}门诊已预约成功,您的预约日期为{2},就诊卡号为{3},请您及时就诊。
sms.reserve.exam.create=您预约的{0}{1}检查已预约成功,您的预约日期为{2},就诊卡号为{3},请您及时就诊。
sms.reserve.reg.cancel=您已经成功取消了{0}{1}{2}的门诊
sms.reserve.exam.cancel=您已经成功取消了{0}{1}{2}的检查
sms.referral.sucess = 您上转成功!上转医院:{0} 预约时间:{1} 就诊卡号:{2}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值