简体   繁体   English

在Java11模块中使用@PostConstruct或@PreDestroy

[英]Using @PostConstruct or @PreDestroy in Java11 module

I am implementing a module for Java11 and want to use annotations from JSR250 (javax.annotation.PostConstruct and javax.annotation.PreDestroy). 我正在为Java11实现模块,并希望使用JSR250中的注释(javax.annotation.PostConstruct和javax.annotation.PreDestroy)。

I already changed the according dependency from: 我已经从以下位置更改了依赖项:

    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>jsr250-api</artifactId>
      <version>1.0</version>
    </dependency>

to this: 对此:

   <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
    </dependency>

That artifact javax.annotation-api-1.3.2.jar contains in META-INF/MANIFEST.MF this line: 该工件javax.annotation-api-1.3.2.jarMETA-INF/MANIFEST.MF包含以下行:

Automatic-Module-Name: java.annotation

Hence, I tried to add this to my module-info.java file: 因此,我尝试将其添加到我的module-info.java文件中:

  requires java.annotation;

However, I get an error for this import: 但是,我收到此导入错误:

import javax.annotation.PostConstruct;

The error message is: 错误消息是:

The type javax.annotation.PostConstruct is not accessible

I already studied the following related questions but am still unable to resolve my issue: 我已经研究了以下相关问题,但仍然无法解决我的问题:

The error occurred only in Eclipse. 该错误仅在Eclipse中发生。 It actually turned out that other unrelated compile errors where causing some side effects in Eclipse. 事实证明,其他不相关的编译错误会导致Eclipse产生一些副作用。 After resolving those and refreshing and cleaning the project the described error magically disappeared. 解决这些问题并刷新和清理项目后,所描述的错误神奇地消失了。 Special thanks to @howlger and @Naman for pointing me in the right direction. 特别感谢@howlger和@Naman为我指出正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM