Spring @Autowired in Servlet [英] Spring @Autowired in Servlet
问题描述
我在我的应用程序中使用Spring框架(2.5.4)加载时间编织,一切正常(在Spring bean中,在非Spring实体中),除非我尝试在注释为@的servlet中自动装配字段可配置,然后我得到一个很好的NullPointerException ...
I am using Spring framework (2.5.4) in my app with Load time weaving and everything works fine everywhere (in Spring beans, in non-Spring entities), except when I try to autowire field in a servlet annotated as @Configurable, then I get a nice 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" />
关于我做错了什么的任何建议?
Any suggestions about what am I doing incorrectly?
谢谢。
推荐答案
另见邮件列表讨论和错误报告https:// bugs.eclipse.org/ bugs / show_bug.cgi?id = 317874。我同意直观地说,servlet上的 @Configurable
注释应该足以向spring框架表明实例化后的servlet将由spring配置,当使用<$ c $时C><上下文:弹簧,其构造-/> 。我还观察到,当使用-javaagent:/path/to/aspectjweaver.jar而不是spring-instrument * .jar或spring-agent.jar时,可以实现所需的行为。请通过https:// jira.springframework.org/browse/SPR向Spring Jira提出问题。我相信问题可能是servlet类 - 不是servlet的实例,而是类本身 - 在调用Spring ContextLoaderListener之前加载,因此spring框架没有机会在它之前检测servlet类。加载。
See also mailing list discussion and bug report at https:// bugs.eclipse.org/bugs/show_bug.cgi?id=317874 . I agree that intuitively the @Configurable
annotation on the servlet should be enough to indicate to the spring framework that the servlet when instantiated will be configured by spring, when using <context:spring-configured/>
. I have also observed that the desired behavior is achievable when using the -javaagent:/path/to/aspectjweaver.jar instead of spring-instrument*.jar or spring-agent.jar. Please raise an issue with Spring Jira at https:// jira.springframework.org/browse/SPR. I believe that the problem may be that the servlet class - not an instance of the servlet, but the class itself - is loaded before the spring ContextLoaderListener is called, thus the spring framework does not have a chance to instrument the servlet class before it is loaded.
加载时编织的弹簧检测似乎是基于能够在加载之前转换类字节码。如果servlet容器持有在Spring转换之前获得的Class对象的实例,那么它(servlet容器)将无法生成转换类的实例,也无法生成实例使用该Class对象上的工厂方法创建。
The spring instrumentation for load-time-weaving appears to be based on being able to transform class bytecode before it is loaded. If the servlet container is holding onto an instance of the Class object that was obtained before it was transformed by spring, then it (the servlet container) would not be able to produce instances of the transformed class, nor would spring be able to instrument instances created using the factory methods on that Class object.
这篇关于Spring @Autowired in Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!