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

查看:153
本文介绍了为什么不在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.

我在网上找到了几种方法来实现这个工作,一个是通过使用一个基本的servlet类来完成一些Spring逻辑,但这样做意味着每个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天全站免登陆