• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Some help required for understanding spring lifecycle

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my LifeCycleBean.java file as follows,



my bean-cfg.xml file as follows,


my MainPractise02.java file were main method is present , is as follows,


now the ouput on command prompt as follows,


my questions are,

why method postProcessBeforeInitialization() is called everytime whenever other beans declared in bean-cfg.xml are initialised ?,
why methods postProcessAfterInitialization() and destroy() were not called ?

wiating for your reply

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You are using BeanFactory instead of your instance variable being ApplicationContext, probably why the post of the BeanPostProcessor doesn't get called.

2) a BeanPostProcessor will always receive every bean declared in your configuration because that is what BeanPostProcessors are about, they post process all your beans. Basically they get passed the entire Map of beans and loops through them all to see if they need to do something to that bean.

Mark
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't understood the first point,

Are you saying that as i'm using BeanFactory thats the reason postProcessesAfterinitialisation() and destroy() methods are not getting called ?

Can you explain me , please
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Shivsharan wrote:i didn't understood the first point,

Are you saying that as i'm using BeanFactory thats the reason postProcessesAfterinitialisation() and destroy() methods are not getting called ?

Can you explain me , please



The BeanFactory is the older way to use Spring, it used to be the Spring Container that was used. Then Spring introduced the ApplicationContext, which added new features including more steps into the Initialization Phase, which is what you are looking at. The BeanPostProcessor step was not part of the BeanFactory initialization lifecycle. But it is with ApplicationContext. ApplicationContext is a Bean Factory plus more.

The destroy method only gets called when you call close() on the ApplicationContext, and that method is only on the ConfigurableApplicationContext interface, so you would have to cast your ApplicationContext to that type, then call close().

Mark
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the code as you said but still it gives a nullpointer exception,

I did the changes in file MainPractise04.java ,


and the command prompt shows
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i removed the try catch and did changes as you said,

my changed file MainPractise04.java,


but still it gives an exception as nullpointer


you can see, the bean Juggler is getting initialised the sop is printed,
so than why spring container is not able to invoke method perform() ?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, looking at your code again.



I would have both those methods return bean and not null. So you basically nulled it out yourself.

So instead use



Also, get rid of those first two lines in your main method. You don't need to have

ApplicationContext context = null;

Just combine it with your instantiation of those two objects.

so just




Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic