This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

@PostConstruct throwing errors  RSS feed

 
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?
 
Master Rancher
Posts: 4072
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: 10206
216
  • 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: 10206
216
  • 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: 21741
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!