【发布时间】:2010-11-15 12:08:24
【问题描述】:
我在我的应用程序中使用 Spring 框架 (2.5.4) 和 Load time weaving 并且一切工作正常(在 Spring bean 中,在非 Spring 实体中),除非我尝试在注释为 @ 的 servlet 中自动装配字段可配置,然后我得到一个不错的 NullPointerException...
@Configurable(dependencyCheck=true)
public class CaptchaServlet extends HttpServlet{
@Autowired
private CaptchaServiceIface captchaService;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
// captchaService = (CaptchaServiceIface) ctx.getBean("captchaService");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Captcha c = captchaService.getCatpcha();
req.getSession().setAttribute("captchaAnswer", c.getAnswer());
resp.setContentType("image/png");
ImageIO.write(c.getImage(), "png", resp.getOutputStream());
}
}
<context:load-time-weaver/>
<context:spring-configured/>
<context:component-scan base-package="cz.flexibla2" />
关于我做错了什么有什么建议吗?
谢谢。
【问题讨论】:
-
我不确定,但可能是因为servlet类是由servlet容器而不是spring容器加载的。
-
@abhin4v:加载时编织背后的想法是允许任何东西加载类,而不仅仅是通过 Spring。
-
@malejpavouk,这种行为的最终解决方案是什么,你能分享一下吗?
-
使用 WebContextUtils 手动查找。我在某处读过,这也可以通过使用 aspectj 代理来解决(但没有尝试过)。我也将此作为错误提交到 Spring JIRA 中,但它已被解决为 wontfix(被设计破坏)。
标签: java spring servlets autowired