我们有一个使用 spring-mvc 和 maven 构建系统编写的相当复杂的 Web 应用程序,并希望在前端利用 Grails 的所有功能。 因此,Grails 应用实际上将调用 spring-mvc 应用的服务层以访问其业务逻辑和数据。
我需要一些关于我的架构方法的指导,以在高层次上实现这种集成。 根据我的理解,我需要; - 在我的 BuildConfig.groovy 中添加我的 spring-mvc 应用程序作为编译依赖项。 - 在我的 conf/spring/resources.groovy 中将服务层对象作为服务 bean 公开,并将它们注入(inject)到我的 Controller 中
问题: 我的 spring-mvc 应用程序有很多自己的依赖项(显然必须有),这会导致很多依赖项错误。我应该在我的配置中设置“transitive=false”并在我的 Grails 应用程序中调用所有这些吗? 应该如何配置数据源?我想我必须通过从我的 Grails applicationContext 调用它来集成我的 spring-mvc 应用程序的 applicationContext,并希望它能很好地引导?
最佳答案
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data
您能否更具体地说明您想从 Grails 使用 Spring MVC 的哪些组件,它只是服务和数据源吗?
I will need to add my spring-mvc app as a compile dependency in my BuildConfig.groovy
是的
Expose the service layer objects as service beans in my
conf/spring/resources.groovy
虽然您可以通过在 resources.groovy
中单独定义 Spring bean 来让您的 Grails 应用程序知道它们,但这是不必要的,因为您已经在 Spring XML 文件中(大概)定义了它们Spring MVC 项目。
相反,您可以使用 importBeans method使用 BeanBuilder 将此 XML 文件中定义的 Spring bean 导入 Grails 应用程序。将 Spring MVC 项目添加为 Grails 应用程序的依赖项后,Spring XML 文件应该位于类路径中,因此您需要做的就是将以下内容添加到 resources.groovy
beans = {
importBeans('classpath:/path/to/file/applicationContext-services.xml')
}
How should the datasource get configured?
名为 dataSource
的 Spring bean 定义了 Grails 应用程序使用的数据源。在标准的 Grails 应用程序中,这个 bean 是基于 DataSource.groovy
中的配置创建的。如果您的 Spring MVC 应用程序定义了一个具有此名称的 bean,则应在进行上述更改后改用此名称。为确保 Grails 使用的是来自您的 Spring MVC 应用程序的数据源,而不是 DataSource.groovy
中的任何内容,我想您可以删除后者的内容。
关于java - Grails 应用程序使用来自 spring-mvc 后端的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18787596/