我正在同时学习 Spring 和 Java。我正在处理我的应用程序上下文。这是我的一颗豆子:
package com.example.app.context;
@Configuration
public class ApplicationContextConfiguration {
@Bean
public ComboPooledDataSource comboPooledDataSource() {
// ... setup pool here
return pool;
}
}
现在我想使用这个bean:
package com.example.db.queries;
import javax.inject.Inject;
public class DatabaseQueries {
@Inject private ComboPooledDataSource comboPooledDataSource;
public static List<Records> getData() {
Connection connection = comboPooledDataSource.getConnection();
// ... create sql query and execute
}
但我在编译时收到此错误:
[ERROR] non-static variable comboPooledDataSource cannot be referenced from a static context
我如何访问这个bean?
在此先感谢,请记住,我正在学习!