springgroovyspring-bootgroovy-sql

Spring Boot app that uses Groovy for JDBC


I have spring-boot application that exposes ReST APIs. I am considering offloading all SQL reads to Groovy SQL. The DataSource is configured in Spring. I would like Groovy to use this DataSource. Btw, there will be multiple DataSource objects (connecting to different datbases).

What is the best approach for this - I want best of both worlds (DI and groovy simplicity). The App has to be in Spring (for project reasons) and will be deployed in WebLogic server utilizing WebLogic defined Data sources.

My idea is to call a Groovy method as shown below from the ReST controller method of Spring-boot:

/student/id
Student getStudentDetails(int id){
    @Autowired
    @Qualifier("someDataSource");
    DataSource myDs;
    Student student = GroovySqlUtil.findStudentById(id, myDs); // pass the DS to Groovy sothat GrooySQL can use the DS for executing queries.
    return student;
}

Is there a better way? Can Groovy directly handle Data sources (multiple DDs)? In that case I won't initialize DataSource in Spring configuration.

There's no requirement for transactions, JPA etc. It's pure SQL read operations.


Solution

  • In fact Groovy, Java, Gradle and Spring-boot can be mixed without any issues. I create a new ReST service in Groovy class and utilized groovy.Sql. Everything works fine.

    Just that the Gradle build file need below configuration:

    sourceSets {
        main {
            groovy {
                // override the default locations, rather than adding additional ones
                srcDirs = ['src/main/groovy', 'src/main/java'] 
            }
            java {
                srcDirs = [] // don't compile Java code twice 
            }
        }
    }
    

    And also have the groovy package in component scan of main Spring configuration class.