从 Groovy class 创建 Spring bean 导致 "cannot find symbol error"
Creating a Spring bean from a Groovy class leads to "cannot find symbol error"
我有一个 groovy class returns 一个数据库客户端的实例:
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")
}
}
我尝试在 @Configuration class:
中创建这个 class 的 bean
@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;
}
}
在 IntelliJ 中看起来不错,但是当我尝试使用 gradle 构建项目时,出现错误
"OracleServerConfiguration.java:14: 错误: 找不到符号
DBClient dbClient = DBClient.getInstance();符号:class DBClient 位置:class OracleServerConfiguration".
我尝试为 DBClient class 中的所有字段创建 getter 和 setter,但无济于事。
有人有从 groovy classes 创建 bean 的经验吗?
这里回答了一个类似的问题:Referencing Groovy from Java with Gradle and Spring Boot results in"cannot find symbol"
就我而言,我只是决定将模块完全保留在 Groovy 中,这解决了问题。
我有一个 groovy class returns 一个数据库客户端的实例:
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")
}
}
我尝试在 @Configuration class:
中创建这个 class 的 bean@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;
}
}
在 IntelliJ 中看起来不错,但是当我尝试使用 gradle 构建项目时,出现错误
"OracleServerConfiguration.java:14: 错误: 找不到符号 DBClient dbClient = DBClient.getInstance();符号:class DBClient 位置:class OracleServerConfiguration".
我尝试为 DBClient class 中的所有字段创建 getter 和 setter,但无济于事。
有人有从 groovy classes 创建 bean 的经验吗?
这里回答了一个类似的问题:Referencing Groovy from Java with Gradle and Spring Boot results in"cannot find symbol" 就我而言,我只是决定将模块完全保留在 Groovy 中,这解决了问题。