Sto usando Spring framework (2.5.4) nella mia app con Load time weaving e tutto funziona bene ovunque (in Spring bean, in entità non Spring), tranne quando cerco di eseguire il campo autowire in un servlet annotato come @Configurable, allora ottengo un bel NullPointerException ...Spring @Autowired in 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" />
Qualche suggerimento su quello che sto facendo in modo errato?
Grazie.
Non sono sicuro, ma potrebbe essere perché la classe servlet sia caricata dal contenitore servlet e non dal contenitore della molla. –
@ abhin4v: L'idea alla base della tessitura a tempo di caricamento è consentire a qualsiasi cosa di caricare la classe, non solo da Spring. – skaffman
@malejpavouk, qual è la soluzione finale per questo tipo di comportamento, puoi condividerla? –