• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

@PostConstruct throwing errors

 
Greenhorn
Posts: 5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to spring. I tried building my own class with annotations. The @PostConstruct annotation is throwing an error-  PostConstruct cannot be resolved to a type.

I have included all the necessary Spring JAR into my classpath. I am running Java 11 with Spring 5.1.7

Where am I going wrong?
 
Rancher
Posts: 4113
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the relevant bit of code, and the import statement at the top of the code?
 
karthik-a- bhat
Greenhorn
Posts: 5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Can you show the relevant bit of code, and the import statement at the top of the code?




I have attached screenshots of my XML file and the class where I am getting errors.
Screenshot-from-2019-06-05-15-47-55.png
[Thumbnail for Screenshot-from-2019-06-05-15-47-55.png]
Screenshot-from-2019-06-05-15-44-46.png
[Thumbnail for Screenshot-from-2019-06-05-15-44-46.png]
 
Saloon Keeper
Posts: 10302
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't imported the annotation yet. You need to import javax.annotation.PostConstruct.
 
karthik-a- bhat
Greenhorn
Posts: 5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You haven't imported the annotation yet. You need to import javax.annotation.PostConstruct.



I am not using any tool like maven to download my JAR files. I have downloaded the JAR's from the spring website and added them to my build path manually.

Isn't javax.annotation.*; included in those JAR's? If not, where do I download the files from?
 
karthik-a- bhat
Greenhorn
Posts: 5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all your answers!

I found the JAR file for Common Annotations on the JCP website and included it as an external JAR in my build path.

https://jcp.org/aboutJava/communityprocess/final/jsr250/index.html

Both the @PostConstruct and the @PreDestroy are working fine!
 
Ranch Hand
Posts: 85
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link may be of interest to you, especially item 4:    https://www.baeldung.com/spring-postconstruct-predestroy
 
Stephan van Hulst
Saloon Keeper
Posts: 10302
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except you might want to use the new Java EE Maven coordinates:
 
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 10 or before, you actually don't have to add any dependency, because it was part of the core Java libraries. For Java 9 and 10 you needed to add module java.xml.ws.annotation but that was enough. They removed this module (and all other JEE modules) in Java 11, so you do need that dependency if you're using Java 11 or higher.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!