Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The import javax.annotation.PostConstruct cannot be resolved [duplicate]

When i import the following packages

import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;

i got the follwing resulat - The import javax.annotation.PostConstruct cannot be resolved

like image 749
Banjit Das Avatar asked Dec 09 '18 07:12

Banjit Das


People also ask

What is PostConstruct in spring?

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 the use of javax annotation?

The two possible authentication types for a resource. The Generated annotation is used to mark source code that has been generated. The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

Should PostConstruct be public?

If a PostConstruct interceptor method returns a value, it is ignored by the container. The method on which PostConstruct is applied MAY be public, protected, package private or private. The method MUST NOT be static except for the application client. The method MAY be final.

Is @PostConstruct deprecated?

In jdk9 @PostConstruct and @PreDestroy are in java. xml. ws. annotation which is deprecated and scheduled for removal.


1 Answers

If you are using spring-boot and below dependency then it should add javax.annotation automatically from parent starter.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

For non spring boot starter data jpa add below dependency in pom.xml

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
</dependency>
like image 194
Alien Avatar answered Nov 14 '22 22:11

Alien