系统框架中加载资源文件几种方式
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);
}
}
}
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);
}
}
}
相关文章推荐
- Spring中加载外部资源文件的几种方式
- java加载资源文件的几种方式
- Android Volley框架的几种post提交请求方式及上传文件(Https单双向认证)
- Struts2的国际化和访问资源文件的几种方式
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- OGRE加载资源文件框架
- SpringMVC加载配置Properties文件的几种方式
- U-boot、Kernel、文件系统的几种下载方式总结(基于TQ2440开发板)
- java文件路径几种加载方式
- uboot参数简介及启动根文件系统的几种方式
- spring加载hibernate映射文件的几种方式
- Android加载图片资源的几种方式
- Android 加载布局文件的几种方式
- 加载资源文件的三种方式
- Spring 创建 IOC 容器时加载配置文件的几种方式
- 常见的几种光盘文件系统和刻录方式
- spring加载hibernate映射文件的几种方式
- Qt 加载QML 文件的几种方式
- ServletContext加载资源文件的方式
- spring加载hibernate映射文件的几种方式