Win a copy of Java Challengers this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

@PostConstruct throwing errors

 
Greenhorn
Posts: 5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 12891
280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 92
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 12891
280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except you might want to use the new Java EE Maven coordinates:
 
Sheriff
Posts: 22152
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic