在项目启动时加载
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}
分享到:
相关推荐
java读取配置文件 在经常更换的一些东西 就可以用配置文件
利用java可以读取配置文件信息,可以流来处理这个问题
在web项目中读取yml配置文件的工具类.可以实现将 server : port : portnumber : 8081 转换为 key为"server.port.portnumber",值为"8081"的Map,String>集合
java如何读取properties配置文件
使用Java读取XML配置文件
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
在项目中我们经常要把某些常量放在配置文件中,这样修改起来会比较方便,这个工具类可以帮我们直接在java类中读取配置文件内容
该资源详细介绍了Java如何读取各种配置文件。如xml ini 等等
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
这个工程是使用 java 来使用反射的原理读取 *.proproies 资源文件中的一些配置信息。利用此方法可以在没有使用框架的情况下可以让你的程序更加灵活易于修改。如:可以将一些数据库配置信息,重要信息写入到资源文件...
纯java底层读取配置文件,生成map,可以根据key模糊匹配查询,封装好了方法。比如getVal("spring.dubbo*url")
JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码
Java中XML配置文件的读取(sax)
在java类里直接读取配置文件中的信息,用到的方法是this.getClass().getClassLoader().getResourceAsStream
最近写一个小程序,需要读取保存在配置文件中的配置信息。 这是一个简单的例子,可以以这个程序为基础写一个完整的类出来。
java读取、更新ini配置文件,可以分块配置