Google Guice、Google Gin和Spring

Google Guice、Google Gin和Spring,spring,gwt,guice,gwt-gin,Spring,Gwt,Guice,Gwt Gin,我计划改进已经编写的代码,这是一个GWT应用程序,需要部署在GAE上 依赖注入由Guice和Gin负责。我想知道我是否可以在后端使用弹簧(这是一种严格的要求) 我让客户机代码正常工作,并向服务器代码发送请求,在服务器代码中的“Service”类中,我想为DAO层进行Spring注入 但不幸的是,即使我进行了@Autowired注入,DAO引用也是空的。这导致了NPE 我知道您只能在spring上下文中注入spring管理的bean。因此,我尝试在服务器端类上放置一个annotation@Serv

我计划改进已经编写的代码,这是一个GWT应用程序,需要部署在GAE上

依赖注入由Guice和Gin负责。我想知道我是否可以在后端使用弹簧(这是一种严格的要求)

我让客户机代码正常工作,并向服务器代码发送请求,在服务器代码中的“Service”类中,我想为DAO层进行Spring注入

但不幸的是,即使我进行了
@Autowired
注入,DAO引用也是空的。这导致了NPE

我知道您只能在spring上下文中注入spring管理的bean。因此,我尝试在服务器端类上放置一个annotation@Service,该类从客户端代码接收RPC请求。该类看起来如下所示:

@Path(ResourcesPath.PERSON)
@Produces(MediaType.APPLICATION_JSON)
@Service
public class PersonResource {

private Logger logger;

@Autowired
PersonDAO dao;

@Inject
PersonResource(Logger logger) {
    this.logger = logger;
}
}
我希望有这样的事情

@Path(ResourcesPath.PERSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {

private Logger logger;

@Inject
PersonResource(Logger logger) {
    this.logger = logger;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="org.example"/>

</beans>

谢谢你的帮助。请给我一些可以解决这个问题的建议。

要在config spring中使用@Service注释,您必须像这样配置您的上下文

@Path(ResourcesPath.PERSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {

private Logger logger;

@Inject
PersonResource(Logger logger) {
    this.logger = logger;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="org.example"/>

</beans>

有关更多信息,请参阅文档。

您可以通过如上所述或中所述配置Spring上下文来完成此操作

许多库可以帮助您将GWT RPC机制与Spring集成, i、 e或

更多的例子可以在网上找到