zoukankan      html  css  js  c++  java
  • Spring 加载配置文件的方式

    我们常用的加载context文件的方法有如下三个:

    1、FileSystemXmlApplicationContext

    这个方法是从文件绝对路径加载配置文件,例如:

    ApplicationContext ctx = new FileSystemXmlApplicationContext( "G:/Test/applicationcontext.xml ");

    如果在参数中写的不是绝对路径,那么方法调用的时候也会默认用绝对路径来找,我测试的时候发现默认的绝对路径是eclipse所在的路径。

    采用绝对路径的话,程序的灵活性就很差了,所以这个方法一般不推荐。

    (如果要使用classpath路径,需要加入前缀classpath:   )

    2、ClassPathXmlApplicationContext

    这个方法是从classpath下加载配置文件(适合于相对路径方式加载),例如:

    ApplicationContext ctx = new ClassPathXmlApplicationContext( "/applicationcontext.xml ");

    该方法参数中classpath: 前缀是不需要的,默认就是指项目的classpath路径下面;这也就是说用ClassPathXmlApplicationContext时默认的根目录是在WEB-INF/classes下面,而不是项目根目录。这个需要注意!

    3、XmlWebApplicationContext

    专为web工程定制的方法,推荐Web项目中使用。例如:

    ServletContext servletContext = request.getSession().getServletContext();

    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

  • 相关阅读:
    angular的路由例子
    angular自定义module
    docker配置phpadmin需要注意的地方
    linux下钉钉,微信
    debian shell脚本关联
    debian下安装带界面的qemu
    ros的一些设置
    新闻排重方案设计
    细解动态规划(一)
    漫画谈-微积分(二)
  • 原文地址:https://www.cnblogs.com/monjeo/p/8066150.html
Copyright © 2011-2022 走看看