用"@Async"批注的方法必须是可重写的



>Intellij 显示红色下划线。
当我将鼠标悬停在红色下划线上时,会显示此消息。

用"@Async"批注的方法必须是可重写的

报告代码阻止类的情况 在运行时由某些框架(例如 Spring 或 Hibernate)子类化

我应该怎么做才能删除此错误?
它显示红色下划线。但它仍然可以工作而没有编译错误。

我正在使用Intellij 2017.2.5。

@Async
private void deleteFile(String fileName, String path) {
    BasicAWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion("ap-northeast-2").withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
    try {
        s3client.deleteObject(new DeleteObjectRequest(AWS_BUCKET_NAME, path + fileName));
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}
@Async指示

Spring 异步执行此方法。所以它只能在几个条件下工作:

  1. 该类必须由 Spring 管理
  2. 该方法必须是公开的
  3. 该方法必须使用 Spring 调用

对于后者,似乎你直接在课堂上调用了这个方法,所以 Spring 没有办法知道你调用了这个方法,这不是 htat 魔法。

您应该重构代码,以便在由 Spring 管理的 Bean 上调用该方法,如以下代码:

@Service
public class AsyncService {
    @Async
    public void executeThisAsync() {...}
}
@Service
public class MainService {
    @Inject
    private AsyncService asyncService;
    public mainMethod() {
        ...
        // This will be called asynchronusly
        asyncService.executeThisAsync();
    }
    ...
}

该错误指示private必须protected cq。 public,用于异步性。

然后看不到异步工具使用此方法。只需添加一个SuppressWarnings,实际上说明您知道自己在做什么。

@Async
@SuppressWarnings("WeakerAccess")
protected void deleteFile(String fileName, String path) {

您可能会向IntelliJ团队提供提示。

最新更新