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


项目中经常需要加载资源文件,总结几种方式仅提供参考


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);
        }
    }
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值