Search code examples
javaspringgradlegroovyjavabeans

Creating a Spring bean from a Groovy class leads to "cannot find symbol error"


I have a groovy class that returns an instance of a data base client:

class DBClient {

private static DBClient INSTANCE

private String url
private String userName
private String password
private String driver

@Synchronized
static DBClient getInstance() {
    if (!INSTANCE) {
        INSTANCE = new DBClient()
    }
    return INSTANCE
}

 Sql sql = Sql.newInstance(url, userName, password, driver)

void setUrl(String url) {
    this.url = url
}

void setUserName(String userName) {
    this.userName = userName
}

void setPassword(String password) {
    this.password = password
}

void setDriver(String driver) {
    this.driver = driver
}


def getAllSaa() {
    sql.rows("select * from Saa")
}
}

I try to create a bean of this class in a @Configuration class:

@Configuration
@Profile("prod")
public class OracleServerConfiguration {

@Bean
public DBClient dbClient () {

    DBClient dbClient = DBClient.getInstance();

    dbClient.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
    dbClient.setUserName("SYSTEM");
    dbClient.setPassword("1111");
    dbClient.setDriver("oracle.jdbc.OracleDriver");

    return dbClient;
}
}

It looks ok in IntelliJ, but when I try to build the project with gradle, I get an error

"OracleServerConfiguration.java:14: error: cannot find symbol DBClient dbClient = DBClient.getInstance(); symbol: class DBClient location: class OracleServerConfiguration".

I tried creating getters and setters for all the fields in DBClient class, but to no avail.

Does someone have experience with creating beans from groovy classes?


Solution

  • A similar question was answered here: Referencing Groovy from Java with Gradle and Spring Boot results in"cannot find symbol" In my case, I just decided to keep the module fully in Groovy, which fixed the problem.