为什么不能在 Spring 中自动装配 GWT servlet 中的字段? [英] Why doesn't just autowiring a field in a GWT servlet in Spring work?

查看:22
本文介绍了为什么不能在 Spring 中自动装配 GWT servlet 中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 GWT servlet 中简单地将字段标记为 @Autowired 不能按预期工作.代码将编译并且 Web 应用程序将启动 - 这意味着 Spring 能够成功地自动装配该字段,但是当 servlet 实际被客户端代码命中时,它将产生一个 NullPointerException - 就像有一个不同的、未初始化的 servlet 副本被命中.

Simply marking a field as @Autowired in a GWT servlet does not work as intended. The code will compile and the web application will start up - which means Spring was successfully able to autowire the field, but when the servlet is actually hit by client-side code, it will yield a NullPointerException - like there's a different, uninitialized copy of the servlet being hit.

我在网络上找到了几种方法来实现这一点,一种是使用执行一些 Spring 逻辑的基本 servlet 类,但这样做意味着每个 GWT servlet 都必须扩展这个基类.另一种方法是使用 AspectJ 和 @Configurable Spring 注释.这里只涉及很少的配置,而且它神奇地起作用了.

I've found several ways on the web to get this working, one is by using a base servlet class that does some Spring logic but doing this means every GWT servlet must extend this base class. The other way was by using AspectJ and the @Configurable Spring annotation. There was very little configuration involved here and it just magically worked.

我的问题是为什么不只是自动装配该字段就可以按预期工作?GWT 正在做什么导致它中断.

My question is why doesn't just autowiring the field just work as intended? What is GWT doing that causes this to break.

推荐答案

事实证明,至少在使用 Spring 时,有一种更简单的方法可以做到这一点,这样您就可以使用 @Autowired 并且不涉及大量配置或基类.需要注意的是,您还必须使用 AspectJ.以下是您的 GWT servlet 所需的:

It turns out that when using Spring at least, there's a MUCH simpler way to do this such that you can use @Autowired and it doesn't involve massive configuration or base classes. The caveat is that you must also use AspectJ. Here's what you need for your GWT servlet:

@Configurable
public class MyGwtServiceImpl extends RemoteServiceServlet implements MyGwtService
{
  @Autowired
  private MyService service;

  // ...
}

在你的 Spring 配置中确保你也有:

And in your Spring config make sure you also have:

   <!-- enable autowiring and configuration of non-spring managed classes, requires AspectJ -->
   <context:spring-configured/>

最后一点.如果您还在 GWT 应用程序中(以及在您的 GWT servlet 中)使用 Spring 安全性,您将需要确保定义正确的模式以确保 AspectJ 编织正确完成(即,您同时获得 @Secured 注释处理和@Autowired 处理)您将需要:

One final note. If you are also using Spring security with your GWT application (and in your GWT servlets), you will need to make sure you define the correct mode to ensure the AspectJ weaving is done correctly (i.e., you get both @Secured annotation processing AND the @Autowired processing) you will need:

   <!-- turn on spring security for method annotations with @Secured(...) -->
   <!-- the aspectj mode is required because we autowire spring services into GWT servlets and this
        is also done via aspectj. a server 500 error will occur if this is changed or removed. -->
   <security:global-method-security secured-annotations="enabled" mode="aspectj"/>

这篇关于为什么不能在 Spring 中自动装配 GWT servlet 中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆