1

我正在尝试使gwt-2.7spring-4.2.3一起使用。配置是:

web.xml

<!-- spring config -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring GWT integration -->
<servlet>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <url-pattern>/idp_web/service/*</url-pattern>
</servlet-mapping>

应用程序上下文.xml

<beans 
...
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
....
default-lazy-init="true">

<!-- auto-inject bean by annotation mechanism -->
<context:component-scan
    base-package="com.vsi.idp.analysis.server,
                    com.vsi.idp.base.server,
                    com.vsi.idp.kpi.server,
                    com.vsi.idp.map.server,//SeniorQueryServiceImpl is under this package
                    com.vsi.idp.statistics.server" />

     //other configurations
</beans>

GWT 服务

@RemoteServiceRelativePath("service/querySenior")
public interface SeniorQueryService extends RemoteService{...}

服务内涵

@Service("querySenior")
public class SeniorQueryServiceImpl extends RemoteServiceServlet implements SeniorQueryService{...}

Spock单元测试工作正常

@ContextConfiguration(locations = "file:war/WEB-INF/applicationContext.xml")
public class SeniorQueryServiceImplTest extends Specification{

  @Autowired
  SeniorQueryServiceImpl service

  def "query by full address"(){
      //blabla
  }
}

运行 gwt 项目告诉:

Failed to load resource: the server responded with a status of 500 (Server Error)

错误堆栈如下所示:

[WARN] Exception while dispatching incoming RPC call
java.lang.IllegalArgumentException: Spring bean not found: querySenior
at org.spring4gwt.server.SpringGwtRemoteServiceServlet.getBean(SpringGwtRemoteServiceServlet.java:96)
at org.spring4gwt.server.SpringGwtRemoteServiceServlet.getBean(SpringGwtRemoteServiceServlet.java:55)
at org.spring4gwt.server.SpringGwtRemoteServiceServlet.processCall(SpringGwtRemoteServiceServlet.java:31)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373)

我认为:

1,"500(server error)" tells that gwt has recognized spring service
2,spring service unit test works fine,so spring configuration is right 

问题可能来自spring4gwt,如何解决这个问题?

4

1 回答 1

-1

这确实应该是一个可行的解决方案。

SpringGwtRemoteServiceServlet#getBean

protected Object getBean(String name) {
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    if (applicationContext == null) {
        throw new IllegalStateException("No Spring web application context found");
    }
    if (!applicationContext.containsBean(name)) {
        throw new IllegalArgumentException("Spring bean not found: " + name);
    }
    return applicationContext.getBean(name);
}

我们可以看到 Exception 的出现是因为 applicationContext 中没有 bean

尝试在 applicatinContext 中隐式声明这个 bean。


推荐

如果你喜欢 RPC,我建议你看看 GWTP 和他们的 GWT 调度模块。方法与 Spring4Gwt 类似,但使用命令模式进行通信要好得多。

在大型项目中使用常规的 GWT RPC 方法服务会在一个地方将很多方法变得一团糟,并且您不会乐于为任何新方法创建新的 Async 对。

或者最好的方法是使用 JSON 进行通信,避免使用 GWT 序列化方法,您会很高兴并且很容易在以后与您的 App 集成。

于 2015-11-23T19:40:12.787 回答