通过非注入方式获取spring管理的bean

   不好意思,这篇文章不是教大家怎么解决如题的问题,我的确尝试了不少方法,大家如果解决了还请不吝赐教,后来我是用spring的HandlerInterceptorAdapter。
   为了这个问题郁闷了好久,用eclipse开发的一个系统,架构是spring MVC + ibatis,
项目结构:
src
webContent
  --WEB-INF
       --classes
       --config
            --baoqu-servlet.xml
            --application-context.xml
       --web.xml


我写了一个filter,在filter中要调用一个spring管理的bean,找了不少方法也没有解决,网上众说纷纭:
//初始化service

1、ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream("../config/baoqu-servlet.xml");
System.out.println(is);

2、 final String SEP = File.separator;
    final String REGX_TEMP="\\"+SEP;

   String result = TokenFilter.class.getResource("TokenFilter.class").toString();
if(result.startsWith("jar")){
// 当class文件在jar文件中时,返回"jar:file:/F:/ ..."样的路径
result = result.substring(10,result.indexOf("WEB-INF"));
}else if(result.startsWith("file")){
// 当class文件在class文件中时,返回"file:/F:/ ..."样的路径
result = result.substring(6,result.indexOf("WEB-INF"));
}else if(result.startsWith("zip")){
result = result.substring(4,result.indexOf("WEB-INF"));
}
result=result.replaceAll("/", REGX_TEMP);
System.out.print(result);     
ServletContext sc = filterConfig.getServletContext();
System.out.print(sc);
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
        //String path=servletContext.getRealPath("/");  得到的就是 编译后web文件夹的路径
        //String path = this.filterConfig.getServletContext().getRealPath("/");
        //path=path+"/WEB-INF/config";
// ApplicationContext ctx = new ClassPathXmlApplicationContext(path);
System.out.print(ctx);
//ApplicationContext ctx=
         new    FileSystemXmlApplicationContext("/config/baoqu-servlet.xml");

都说文件不存在,无法打开文件。

猜你喜欢

转载自lancezhcj.iteye.com/blog/1151286