您的位置:首页 > 其它

系统框架中加载资源文件几种方式

2015-05-07 16:19 459 查看
项目中经常需要加载资源文件,总结几种方式仅提供参考

1、 在配置文件中获得资源key和value

<context:property-placeholder ignore-unresolvable="true" local-override="true" location="classpath:application.properties"/>

2、用于程序中获得配置参数

<!-- 用于程序中获得配置参数 ,例如:@Value("#{app_properties['export.diagram.path']}")-->

<util:properties id="app_properties" location="classpath:application.properties" local-override="true"/>

@ResponseBody

@RequestMapping(value = "/processDeploy/operate/deploy",method=RequestMethod.POST)

public void deploy(@Value("#{app_properties['export.diagram.path']}") String exportDir,

@RequestParam(value = "Filedata", required = true) MultipartFile file) {

try {

activitiService.deploy(file, exportDir);

} catch (Exception e) {

e.printStackTrace();

logger.error("部署流程文件【"+file.getOriginalFilename()+"】失败",e);

}

}

3、需在jsp中使用资源文件

<%@ page import="java.util.*,com.bing.core.spring.ApplicationContextHelper,org.springframework.beans.factory.config.PropertiesFactoryBean" %>

<%

Properties properties=(Properties)ApplicationContextHelper.getBean("applicationProperties");

String attachType=properties.getProperty("attachType");

String attachSize=properties.getProperty("attachSize");

%>

<script type="text/javascript">

var _attachType="<%=attachType%>";

var _attachSize="<%=attachSize%>";

</script>

xml中配置ApplicationContextHelper

<!-- ApplicationContext环境外获取bean的工具类 -->

<bean id="applicationContextHelper" class="com.bing.core.spring.ApplicationContextHelper"/>

ApplicationContextHelper只需要implements ApplicationContextAware即可。

4、spring加载自定义读取资源文件类

<bean id="initUploadPath" class="com.bing.utils.InitUploadPath" init-method="initPath"></bean>

public class InitUploadPath{

public final static Map<String,String> pathMap = new HashMap<String,String>();

private static final Logger logger= LoggerFactory.getLogger(InitUploadPath.class);

//初始化数据

public void initPath(){

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");

Properties p = new Properties();

try {

p.load(inputStream);

} catch (IOException e) {

e.printStackTrace();

}

if(System.getProperties().getProperty("os.name").indexOf("Win") != -1){

pathMap.put("dirPath", p.getProperty("win_DirPath"));

}else{

pathMap.put("dirPath", p.getProperty("linux_DirPath"));

}

pathMap.put("maxSize", p.getProperty("fileSize"));

pathMap.put("image", p.getProperty("imageExt"));

pathMap.put("flash", p.getProperty("flashExt"));

pathMap.put("media", p.getProperty("mediaExt"));

pathMap.put("file", p.getProperty("fileExt"));

}

//按键值获取数据

public static String getFaqCorrentType(String keys) {

if (keys == null || "".equals(keys)) {

return null;

} else {

return pathMap.get(keys);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: