I upgraded the SDK that my project uses to Java 10. The following import statement caused an error:
import javax.annotation.PostConstruct;
Package 'javax.annotation' is declared in module 'java.xml.ws.annotation', but module 'application' does not read it
Hitting ALT+ENTER to let Intellij fix it, I received the following options:
I selected Add Maven Dependency...
and the following dependency was added to pom.xml.
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
Looking at the newly added JAR's MANIFEST.MF, I noted that the module's name was java.annotation.
So i added requires java.annotation
to module-info.java.
module application {
requires java.annotation;
}
When I CTRL+clicked on requires java.annotation
:
Intellij correctly navigated to the JAR:
So it appeared that I had the module name correct and that javax.annotation:javax.annotation-api
was available to my module. Unfortunately, the error on import javax.annotation.PostConstruct
did not go away. To confirm that my module descriptor was working properly, I added other directives, and all worked fine. I also added the directive requires java.xml.ws.annotation
,
which made the import statement error go away but, of course, this is not a satisfactory solution as java.xml.ws.annotation is deprecated and marked for removal.
Any ideas on how to resolve this problem? I need to be able to make module java.annotation
available to module application
and from what I've read here and here and here and here the way to do it is by adding a JAR to the module path, and from what I've read here the newly added module must be referenced via directive in module-info.java.
When we annotate a method in Spring Bean with @PostConstruct annotation, it gets executed after the spring bean is initialized. We can have only one method annotated with @PostConstruct annotation. This annotation is part of Common Annotations API and it's part of JDK module javax.
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection.
@PostConstruct : is called after the bean has been initialized and before this bean is returned to the requested object. @PreDestroy : is called just before the bean is removed from the container.
The javax. annotation. Resource annotation is used to declare a reference to a resource; @Resource can decorate a class, a field, or a method.
The trouble with IntelliJ IDEA is that it knows about the module in JDK/jmods/java.xml.ws.annotation.jmod
, even though at runtime it will be disabled.
Just comment out the java.xml.ws.annotation
jmod in the Java SDK definitions in IntelliJ IDEA Project Structure.
Add a Maven dependency:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
and module-info
dependency:
requires java.annotation;
as you already did.
Then, stop IntelliJ IDEA, go to your IntelliJ JDK config file (i.e. C:\Users\YOUR_NAME\.IntelliJ2018\config\options\jdk.table.xml
and comment out the line with java.xml.ws.annotation.jmod
reference:
<!-- <root url="jrt://C:/Java-Training/JDK!/java.xml.ws.annotation" type="simple" /> -->
IntelliJ will stop seeing java.xml.ws.annotation
.
After restart, everything will be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With