Java 用“@Async”注释的方法必须是可重写的
Intellij显示红色下划线。 当我将鼠标移到红色下划线上时,会显示此消息 用“@Async”注释的方法必须是可重写的 报告代码阻止类被删除时的情况 在运行时被某些框架(如Spring或Hibernate)子类化 我应该如何删除此错误? 它显示红色下划线。但它仍然可以正常工作,没有编译错误 我正在使用Intellij 2017.2.5Java 用“@Async”注释的方法必须是可重写的,java,spring,hibernate,intellij-idea,Java,Spring,Hibernate,Intellij Idea,Intellij显示红色下划线。 当我将鼠标移到红色下划线上时,会显示此消息 用“@Async”注释的方法必须是可重写的 报告代码阻止类被删除时的情况 在运行时被某些框架(如Spring或Hibernate)子类化 我应该如何删除此错误? 它显示红色下划线。但它仍然可以正常工作,没有编译错误 我正在使用Intellij 2017.2.5 @Async private void deleteFile(String fileName, String path) { BasicAWSCrede
@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());
}
}
该错误指示必须保护私有cq。公共,用于异步性
然后,我们看不到异步工具使用了这种方法。只需添加一个SuppressWarnings,说明您知道自己在做什么
@Async
@SuppressWarnings("WeakerAccess")
protected void deleteFile(String fileName, String path) {
您可能会给IntelliJ团队一个提示。@Async指示Spring异步执行此方法。因此,它只能在几种条件下工作:
该类必须由Spring管理
方法必须是公开的
必须使用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();
}
...
}
将该方法标记为protected或public,并确保该类不是final。如消息所示。@RoddyOfFrozenpeas如果我更改为“受保护”或“公共”,intellij show Access可以是带有黄色背景的私人消息。在这种情况下,intellij似乎有问题。您是否已将此问题提交给他们的支持团队?@JasonMathison No.尚未提交。我应该提交这个问题吗?我不确定“这真的是虫子吗?”。