Friday, June 11, 2010

Chapter 4 - advanced Spring - continued!

Ok. It's 6:45 am. I will work on this stuff until about 9. Let's go!

We left off at 4-10. Bean Post-processors. These sandwich the initialization callback step.

To get to it, add a "StorageConfig" interface (this is a name of your own choosing). Make it implement any methods you will need to add in prep/post initialization. In this case, it's "getPath".

You want "Cashier" to check for an existing path, so this is where you implement "StorageConfig".

So, in your class that implements post processor, you need to implement BeansPostProcessor. This calls will get an instance of all beans. It has a before and after method, both of which must return the bean, even if it doesn't do anything (postProcessBeforeInitialization, postProcessAfterInitialization).

In this case, the before initialization process checks if the object is of a a type of StorageConfig; if so, it will check to make sure a path exists.

Just declare post processing class in the xml. Spring will see it's interface and make sure to pass every bean to it.

There's an issue if you use the annotation method for initialization. It will be called before the post processing because the CommonAnnotationBeansPostProcessor has a higher priority than the BeansPostProcessor. So you need to have you path checking post processor implement the PriorityOrdered interface to beat the CommonAnnotationBeans post processor to the punch.

Then assign it a priority of 0 in you configuration, and you will beat the CommmonAnnotationsBeanPostProcessor to the punch. You can use the "context:annotaton-config" tag instead of the full declaration of CommonAnnotationBeanPostProcessor".

No comments:

Post a Comment