Grails与另一个Spring应用程序集成 - dataSource正在重载 [英] Grails Integration with another Spring app - dataSource being overloaded

查看:117
本文介绍了Grails与另一个Spring应用程序集成 - dataSource正在重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为现有的基于Spring的应用程序构建CRUD工具。
该应用程序正在以一个JAR库的形式被包含在Grails应用程序中,似乎工作正常。

I'm currently in the process of building a CRUD tool for an existing Spring-based application. The application is being included in the Grails app as a JAR library which seems to work fine.

为了利用库自己的spring上下文,我使用

To make use of the library's own spring context, I used to load it through:

def ctx = new ClassPathXmlApplicationContext( 'classpath:/applicationContextName.xml')

。不幸的是,上下文构建了自己的数据源(库的默认),这是不好的,因为我需要使用在Grails中定义的dataSource。

in my service. Unfortunately the context builds its own datasource (the library's default) which is no good, as I need to use the dataSource defined in Grails.

所以,我的解决方案是通过向grails-app\conf\spring\resources.xml文件中添加导入来包含库的弹簧配置和Grails上下文。

So, my solution was to just include the library's spring configuration with the Grails context by adding an import to the grails-app\conf\spring\resources.xml file.

这似乎工作(在所有的bean被加载到相同的上下文中,我现在可以通过使用 def variableName ,将bean直接自动连接到我的服务类。

This seems to work (in so much as all beans are loaded into the same context and I can now autowire the beans straight in to my service classes by using def variableName.

不幸的是,在库的spring配置中定义的dataSource是重载Grails DataSource.groovy文件中定义的dataSource!

Unfortunately, the dataSource defined in the library's spring config is overloading the dataSource defined in my Grails DataSource.groovy file!

方法我可以告诉Grails加载库的弹簧配置,以便它然后被其他Grails config(因此使用Grails的DataSource)覆盖?

Is there any way I can tell Grails to load the libary's spring configuration first, so that it then gets overridden by the rest of Grails config (and thus using Grails' DataSource)?

谢谢为您的帮助,

James

...

作为一个临时措施,我删除了依赖的spring配置文件中的dataSource条目,并且其bean已注入由Grails配置创建的dataSource - 这不是理想的,因为我不得不做一个

As a tenporary measure, I've removed the dataSource entry in the dependancy's spring config file and its beans have been injected the dataSource created by the Grails config - this isn't ideal though, as I've had to make a 'special' build of the dependancy jar.

推荐答案

我会看看制作一个简单的插件包含你的库,并得到它在你的* Plugin.groovy文件中加载dataSources插件之前(使用 def loadBefore = ['datasources'] )。

I'd look at making a trivial plugin containing your library and get it to load before the dataSources plugin (use def loadBefore = ['datasources'] in you *Plugin.groovy file).

每个插件都有一个doWithSpring钩子,允许你添加bean到上下文(你可以包括你现有的context.xml)。

Each plugin has a doWithSpring hook which lets you add beans to the context (you could include your existing context.xml there).

作为一个额外的好处,它将使下次更轻松地重新使用图书馆:

As an added bonus it will make it easier to re-use the library next time :)

Lee

这篇关于Grails与另一个Spring应用程序集成 - dataSource正在重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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