Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij: how to add java.annotation module for javax.annotation.PostConstruct

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

enter image description here

Hitting ALT+ENTER to let Intellij fix it, I received the following options:

enter image description here

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.

enter image description here

So i added requires java.annotation to module-info.java.

module application {
    requires java.annotation;
}

When I CTRL+clicked on requires java.annotation:

enter image description here

Intellij correctly navigated to the JAR:

enter image description here

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,

enter image description here

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.

enter image description here

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.

like image 635
Patrick Garner Avatar asked Aug 29 '18 19:08

Patrick Garner


People also ask

What is PostConstruct annotation in spring boot?

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.

What is PostConstruct Java?

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.

What is PostConstruct and PreDestroy?

@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.

What is javax annotation resource?

The javax. annotation. Resource annotation is used to declare a reference to a resource; @Resource can decorate a class, a field, or a method.


1 Answers

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.

Detail steps:

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.

like image 54
Shine Developer Avatar answered Sep 17 '22 17:09

Shine Developer