springMVC中实现servlet依赖注入

时间:2023-03-09 18:20:43
springMVC中实现servlet依赖注入

记录一下开发过程中遇到的问题:

首先看一下这个帖子: http://blog.****.net/gaogaoshan/article/details/23540129

由于我使用的是springMVC,且组件扫描是放在dispatcherServlet-servlet.xml中的,所以上面的方法是不行的。

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

需要将上面的代码改为:

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext(),

FrameworkServlet.class.getName() + ".CONTEXT.dispatcherServlet");

//".CONTEXT.dispatcherServlet"中的dispatcherServlet为web.xml中springMVC的servlet名称

具体原因查看下面的帖子:

http://www.cnblogs.com/davidwang456/p/4122842.html