PDA

View Full Version : NamingStrategy that supports type key property


sabarish
Jan 15th, 2006, 01:36 PM
I could not find a NamingStrategy that supports generation of a type key property. Would it make sense to add a NamingStrategy for this, especially for applications that register many application beans (for ex all the facade beans) as MBeans?

A NamingStrategy based on a Decorator pattern that would decorate an existing NamingStrategy by adding a type key property to the ObjectName would be a nice to have. If a naming strategy impl to be decorated is not provided, it could fall back onto the logic in the default naming strategy, but then add a type key property.

Usually application classes implement some interface that can denote what kind of class it is. For ex, all facade classes would implement a MyServiceIntf. Similarly all DAOs might implement a Persister interface. So a InterfaceBasedTypeNamingStrategyDecorator that accepts a map (of interface names mapped to type values) would provide an easy way to supply the type property for such beans.

The strategy impl could check if the passed in managedObject implements one of the interfaces in the map fed to the strategy. And if so, it appends a type key property to the ObjectName, the value for which would be the type value from the map for that interface. But if the managedObject is not an instance of any interfaces in the map (or if no map was provided), then the type gets generated from the name of the super interface implemented by the managedObject.

This way using 4 or 5 (or 0) entries in the map, we can generate the type property for all the managed beans in the application. I don't think this would be of use to many apps out there, but still some would benefit :)

robh
Jan 20th, 2006, 10:22 AM
Can you create a JIRA issue for this? Maybe with a code patch as well if you have time?

Rob

sabarish
Jan 20th, 2006, 09:44 PM
A JIRA issue (http://opensource2.atlassian.com/projects/spring/browse/SPR-1635) has been created. An attachment with a sample full blown implementation has been added as well (along with accompanying test cases).

The logic used is
1. if namingstrategy to be decorated exists
---> get object name from it.
---> if that has a type key prop, replace it if the replace flag is set. else simply return the object name
---> else (ie, if no type key prop) generate type key prop and append to object name
2. else (ie no decorated strategy)
---> generate default object name use bean:name=beanKey syntax
---> generate type key prop and append to object name

Logic used to generate type key prop
1. If bean passed is a subtype of one of the interfaces in the interface map, return the value from the map as the type
2. if no entry in map matches or if no map at all, return first super interface name of the bean. if no super interface return class name