我在我的应用程序中使用Spring框架(2.5.4),加载时编织和一切工作正常无处不在(在Spring的bean中,在非Spring实体中),除非我尝试自动装入字段注释为@Configurable一个servlet,然后我得到一个不错的NullPointerException异常...Spring @Autowired在Servlet中
@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容器。 – 2010-11-15 12:27:34
@ abhin4v:加载时织入的思想是允许任何东西加载类,而不仅仅是Spring。 – skaffman 2010-11-15 12:29:11
@malejpavouk,这种行为的最终解决方案是什么,请分享一下吗? – 2012-08-07 10:19:20