Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

about @PostConstruct and @PreDestroy annotation

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all. l have three classes below







if l run this code, got below output  ...




if l changed Bilgisayar




and SpringConfig class like below



output will be like below



you noticed the difference .. in first output  
"@PostConstruct" comes before than "Bilgisayar CONSTRUCTOR"

but in scond one
"Bilgisayar CONSTRUCTOR"  comes before than  "@PostConstruct"


why ?
 
Saloon Keeper
Posts: 11881
253
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The @PostConstruct method is called after an instance of its containing class is constructed. The SpringConfig is constructed before the Bilgisayar is.

So if you put the @PostConstruct method in the SpringConfig class, it will print "@PostConstruct" before it prints "Bilgisayar CONSTRUCTOR". And if you put it in the Bilgisayar class instead, then it will print "@PostConstruct" after it's called the Bilgisayar constructor, as implied by the name 'PostConstruct'.

I don't really know how to make it more obvious than that. What confuses you? What do you expect to happen and why?
 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Stephan  ...
l really grateful for your helpful answer.

What l am want to do ?

Instead of putting "@PostConstruct" and "@PreDestroy" annotation in all bean class,  l want more generic solution, which is  putting  "@PostConstruct" and "@PreDestroy" annotation in
"Configuration" class than expect to work this two methods for all bean classes.

but for only one bean class its name is "Bilgisayar", "@PostConstruct"  method which is the "Configuration" class is not working as l expect.

l dont know could l explaing my point

       

 
Bartender
Posts: 1259
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salih ayan wrote:hi Stephan  ...
What l am want to do ?
Instead of putting "@PostConstruct" and "@PreDestroy" annotation in all bean class,  l want more generic solution, which is  putting  "@PostConstruct" and "@PreDestroy" annotation in
"Configuration" class than expect to work this two methods for all bean classes.



That's not the way Spring beans' lifecycle works.
@PreDestroy and @PostConstruct are applied only to the methods annotated with them, within a specific class.

You may want to define a common ancestor class, with @PreDestroy and @PostConstruct annotated methods, and let your managed beans extend it.


 
Ranch Hand
Posts: 1871
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Salih,
Since your Bilgisayar is a singleton, you only have one bean constructed. So, b1 and b2 refer to the same bean.
The @POSTConstruct method is called after the Bilgisayar bean has been created.
But we cannot control the order of the printing.

 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Claude

You may want to define a common ancestor class, with @PreDestroy and @PostConstruct annotated methods, and let your managed beans extend it.


this came to my mind...
but still l was thinking spring must have special solutions for this situation..
 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Himai

Himai Minh wrote:Hi, Salih,
Since your Bilgisayar is a singleton, you only have one bean constructed. So, b1 and b2 refer to the same bean.
The @POSTConstruct method is called after the Bilgisayar bean has been created.
But we cannot control the order of the printing.



you are telling if we write @PostConstruct methods in confugration class this method while be performed each time after each bean class is constructed but we cant notice this
because printing order to the console  is not controlled by us
 
Bartender
Posts: 1057
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the BeanFactoryPostProcessor interface is what your are looking for.  
Not sure why you would want to extend the actual framework itself, but it is possible.  

 
Himai Minh
Ranch Hand
Posts: 1871
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Salih,
In my opinion, we cannot rely on the order of printing to determine the order of execution.
You may find this tutorial about bean life cycle helpful :https://www.youtube.com/watch?v=A4vrK4PnqV0
 
Claude Moore
Bartender
Posts: 1259
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Rooke wrote:I suspect the BeanFactoryPostProcessor interface is what your are looking for.  
Not sure why you would want to extend the actual framework itself, but it is possible.  



If the OP wants really to do this:

Instead of putting "@PostConstruct" and "@PreDestroy" annotation in all bean class,  l want more generic solution, which is  putting  "@PostConstruct" and "@PreDestroy" annotation in
"Configuration" class than expect to work this two methods for all bean classes.



then creating one's one BeanPostProcessorBean could be a good idea. I was thinking about using a class hierarchy - if you want to use the very same method for doing something, one should put in a single ancestor class the required code.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic