简体   繁体   中英

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?

这里回答了一个类似的问题: 用Gradle和Spring Boot从Java引用Groovy会导致“找不到符号”。就我而言,我决定将模块完全保留在Groovy中,从而解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM