tags:

views:

40

answers:

3

I have a question out of curiosity.. Is it possible to track object created and destroyed by Spring container? What I am thinking of is not logging but some kind of visual representation of objects life cycle.

A: 

Using IoC events you can get some information about when is your bean created or destroyed. I'm not sure, if it's possible to do it globally somwhow as there is more application contexts that can be used.

binary_runner
that's interesting.. let me have a look, thanks
Abdel Olakara
A: 

definitely use @PostConstruct and @PreDestroy. How you go about actually tracking is another matter. Perhaps a Tracker instance which gets injected into the classes you want to track. In the @PostConstruct and @PreDestroy methods, which are called after dependency injection and before dependent objects get destroyed, respectively, you can then call trackerInstance.trackCreation(this) and trackerInstance.trackDestroy(this), or similar.

If you use aspectj and @Autowire dependency injection of the Tracker, you can even track the lifecycle of non-spring managed objects, since aspectj will wire up objects that you create via a call to new.

A: 

Using a BeanPostProcessor you can catch the initialization of the beans. See the examples in the spring docs for details.

abhin4v
Sadly, you can't track the full lifecycle with these, just the initialization.
skaffman