开发者社区> 问答> 正文

java加载spring时找不到配置文件.

背景:因需要处理支付宝的异步通知,所以把处理业务封装成了接口,在jsp上实例化再调用处理相关数据.

问题:实例化的过程中加载不到applicationContext.xml
异常信息screenshot
运行环境:centos,resin(/usr/resin),java1.6

java代码:

public static String ROOT = OrderAction.class.getClassLoader().getResource("").toString().replaceAll("file:/", "").replaceAll("/WEB-INF/classes/", "");
     
     
    public OrderAction(){
        String xmlPath = (System.getProperty("os.name").toLowerCase().indexOf("linux")!=-1?"/":"")+ROOT+"/WEB-INF/conf/applicationContext.xml";
 
        File xmlFile = new File(xmlPath) ;
        if(xmlFile.exists()){
            logger.error("get the file "+xmlPath) ;
            ApplicationContext context = new FileSystemXmlApplicationContext(xmlPath); 
                    taskService =  (ITaskService) context.getBean("taskService") ;
                    commonservice = (CommonService) context.getBean("commonServices") ;
                    logger.error("-----"+taskService==null) ;
                    logger.error("-----"+commonservice==null) ;
        }else{
            logger.error("xml not exit !"+xmlPath) ;
        }
    }

OrderAction oa = new OrderAction() ;
oa.handle(request.getRemoteAddr(),"201408141016588003", "success") ;
applicationContext.xml实际路径是 /usr/shared/webapps/ROOT/WEB-INF/conf/applicationContext.xml
不明白为什么会把resin的路径给加上了.应该和resin无关吧~

展开
收起
a123456678 2016-03-17 13:46:57 2386 0
1 条回答
写回答
取消 提交回答
  • File classFloder = new File(OrderAction.class.getClassLoader().getResource("").getPath()); 
    File confFloder = new File(classFloder.getParentFile(),"conf"); 
    File applicationContext = new File(confFloder,"applicationContext.xml"); 
    2019-07-17 19:05:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载