aspose file tools*
The moose likes Spring and the fly likes Some help required for understanding spring lifecycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Some help required for understanding spring lifecycle " Watch "Some help required for understanding spring lifecycle " New topic
Author

Some help required for understanding spring lifecycle

Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83

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

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83
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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

Joined: Jun 17, 2009
Posts: 83
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

Joined: Jun 17, 2009
Posts: 83
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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Some help required for understanding spring lifecycle