1. web.xml中配置一个继承了ServletContextListener的listner,并且实现contextInnitialized方法
2. 在方法中创建IoC容器
比如创建ClasspathApplicationContext, 还可以通过web.xml中配置应用参数的方式配置spring配置文件的路径。
创建后的IoC容器放入ServletContext.setAttribute中
这样applicationContext.xml文件路径也是可以改变的。
3. 在applicationContext.xml中正常使用spring配置
在需要用到bean的地方, 先获取ServletContext, 然后。。
网友评论