获取Tomcat容器中WEB-INF路径下的文件.
在Servlet中获取WEB-INF下的配置文件.
比如使用:
String path = this.getServletContext().getRealPath("/WEB-INF/applicationContext.xml");
输出的路径:
D:\SOFTWARE\apache-tomcat-6.0.29\apache-tomcat-6.0.29\webapps\wmba\WEB-INF\applicationContext.xml
//初始化spring中的bean实例。
String path = this.getServletContext().getRealPath("/WEB-INF/applicationContext.xml");
ApplicationContext context = new FileSystemXmlApplicationContext(path);
TestBean someBean = (TestBean)context.getBean("testBean");
还有一种就是把spring的配置文件配置在classes文件夹下.
则使用下面这种方式:
如果是两个以上:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});
或者用通配符指定搜索:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
即可.