项目中用到spring容器来管理业务bean,在servlet中就收到前台传递来的请求参数后,调用业务bean,老是出错
部门代码如下
[java] view plaincopy
- <span style="font-size:18px;">private UserService userService;
- public UserService getUserService() {
- return userService;
- }
- @Resource
- public void setUserService(UserService userService) {
- this.userService = userService;
- }</span>
运用@Resource注解注入,老是报错说什么
Name XXX is not bound in this Context
让人郁闷死,排错了好久,而在action中一切都很正常,有谁知道为什么了给我说下,
这里我找了一种解决问题的方法给大家分享:
[java] view plaincopy
- private UserService userService;
- @Override
- public void init(ServletConfig servletConfig) throws ServletException {
- super.init(servletConfig);
- WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
- userService=(UserService) webApplicationContext.getBean("userServiceImpl");
- }
时间: 2024-11-08 01:29:16