spring加载配置文件的方式

 

1、把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式 

 2、采用在web.xml中配置ContextLoaderListenera或ContextLoaderServlet指定加载路径方式。它们两个有着同样的功能,都实现在了org.springframework.web.context.ContextLoader类,都要定义contextConfigLocation参数。区别在于listener不能在Servlet 2.2兼容的容器中使用。自从Servelt 2.4规范,listener被要求在web应用启动后初始化。web.xml初始化的时候,listerner会检查contextConfigLocation参数。如果不存在的话,它将默认使用/WEB-INF/applicationContext.xml。如果它存在,它就会用预先定义的分隔符(逗号,分号和空格)分开分割字符串(<param-value></param-value),并将这些值作为应用上下文将要搜索的位置。  代码  <context-param>        <param-name>contextConfigLocation</param-name>       <param-value>          /WEB-INF/daoContext.xml,         /WEB-INF/config/appContext1.xml,        /WEB-INF/config/appContext2.xml    </param-value>   </context-param>       <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>       <!-- 另一种是使用ContextLoaderServlet    <servlet>            <servlet-name>context</servlet-name>        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>        <load-on-startup>1</load-on-startup>   </servlet>   -->    例如spring MVC中使用contextConfigLocation自动加载value中的配置文件,用'","隔开

<servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/frame/config/springmvc-config/frame-servlet.xml,/framels/config/springmvc-config/framels-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.spring</url-pattern>
 </servlet-mapping>

struts2中用config加载配置文件

<servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/frame/config/ssh-config/struts-config-frame.xml</param-value>
  </init-param>
  <init-param>
   <param-name>application</param-name>
   <param-value>p311ResV10</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>3</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>

 

 3 通过ClassPathXmlApplicationContext或XmlWebApplicationContext代码动态加载  

 一:XmlBeanFactory 引用   

Resource resource = new ClassPathResource("appcontext.xml");   BeanFactory factory = new XmlBeanFactory(resource);  

二:ClassPathXmlApplicationContext 编译路径  

ApplicationContext factory=new  ClassPathXmlApplicationContext("classpath:appcontext.xml");   

 ApplicationContext factory=new  ClassPathXmlApplicationContext("appcontext.xml"); // classpath目录下的

  三 : 用文件系统的路径 

   ApplicationContext factory=new  FileSystemXmlApplicationContext("src/appcontext.xml");  

  //使用了 classpath: 前缀,作为标志, 这 样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径  

  ApplicationContext factory=new  FileSystemXmlApplicationContext("classpath:appcontext.xml");   

 ApplicationContext factory=new  FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");       

 ApplicationContext factory=new  FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");  

 

  四: XmlWebApplicationContext 是专为Web工程定制的。 

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

 ApplicationContext ctx =  WebApplicationContextUtils.getWebApplicationContext(servletContext );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值