Grails Integration with Another Spring Application - Data Source Overloaded

I am currently creating a CRUD tool for an existing Spring application. The application is included in the Grails application as a JAR library that works fine.

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

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

in my service. Unfortunately, the context creates its own data source (the default library), which is not suitable, since I need to use the dataSource defined in Grails.

So my solution was to simply enable the spring library configuration with Grails content by adding import to the grails-app \ conf \ spring \ resources.xml file.

This seems to work (since all beans are loaded in the same context, and now I can autowire beans directly into my service classes using def variableName .

Unfortunately, the dataSource defined in the spring config library overloads the dataSource defined in my Grails file DataSource.groovy!

Is there any way to understand that Grails is loading the libary spring configuration so that it is then overridden by the rest of the Grails configuration (and therefore using the DataSource Grails)?

Thanks for your help,

James

...

As a temporary measure, I deleted the dataSource entry in the spring dependencies configuration file and its beans were injected with the data source created by the Grails configuration, but this is not ideal, since I had to do a β€œspecial” build of the dependency ban.

+4
source share
2 answers

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

Each plugin has a doWithSpring hook that allows you to add beans to the context (you can include the existing context.xml in it).

As an added bonus, this will make it easier to reuse the library next time :)

amuses

Lee

+1
source

A simpler solution would be to import the application context into the Grails context (resources.xml), and then there are two options. You can rename the application data source to be different from Grails, or you can simply remove the dataSource from the application and use one of the Grails part Grails. The difference between the solutions is that the two data sources are connected to the same database or not.

0
source

Source: https://habr.com/ru/post/924645/


All Articles