我更新了父 pom 以使用 Spring Boot 2.1.2 版本。在我修复的其他错误和弃用中,有一个最困扰我:
创建名为 'adminServiceImpl' 的 bean 时出错:通过字段 'taskExecutor' 表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="taskExecutor")}
我的配置类如下所示:
@EnableScheduling
@EnableAsync
@Configuration
@ConfigurationProperties("thread.pool")
public class MyAsyncConfig extends AsyncConfigurerSupport {
...
@Bean(name = "taskExecutor")
@Override
@Primary
public TaskExecutor getAsyncExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.initialize();
return executor;
}
Run Code Online (Sandbox Code Playgroud)
我自动装配的类定义了 executor 字段:
@Autowired
@Qualifier("taskExecutor")
private ThreadPoolTaskExecutor taskExecutor;
Run Code Online (Sandbox Code Playgroud)
这曾经在 springboot 2.0.2 中工作,但是当我移动到 2.1.2 版本时,我得到了
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor”类型的合格bean。
我解决这个问题的一种方法是在配置类中声明getAsyncExecutor()方法的返回类型为 ThreadPoolTaskExecutor. 这样做之后,它就起作用了。但我想知道为什么它不像 2.0.2 那样工作?
请问下面的代码是否可以正确使用连接池(DBCP)?
我提供 BasicDataSource 的实用程序类如下(几乎与 apache 示例相同)
public class DatabaseUtility {
private static BasicDataSource dataSource;
public static BasicDataSource getDataSource(Properties prop) {
if (dataSource == null)
{
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:oracle:thin:@"+ prop.getProperty("db") + ":" + prop.getProperty("dbPort") + "/" +
prop.getProperty("dbService"));
ds.setUsername(prop.getProperty("dbUser"));
ds.setPassword(prop.getProperty("dbPassword"));
ds.setMinIdle(5);
ds.setMaxIdle(10);
ds.setMaxOpenPreparedStatements(100);
dataSource = ds;
}
return dataSource;
}
Run Code Online (Sandbox Code Playgroud)
然后我将上述内容用作:
public class MyClass {
public static boolean isNew(Properties prop, String label) {
Connection connection = null;
PreparedStatement ps = null;
try {
BasicDataSource dataSource = DatabaseUtility.getDataSource(prop);
connection …Run Code Online (Sandbox Code Playgroud)