Spring载入配置文件

今天总结了一下在spring中载入配置文件的方法,写下来以免忘了。
spring的配置文件可以通过web.xml自动载入,配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
也可以通过代码来载入配置文件,spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext, FileSystemXmlApplicationContext和XmlWebApplicationContext。
1.ApplicationContext context = new ClassPathXmlApplicationContext("test/ApplicationContext.xml");
2.String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );
3.ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

猜你喜欢

转载自luanc.iteye.com/blog/1397498