Let’s assume we’ve got the following Java code:
public class Maintainer { private Map<Enum, List<Listener>> map; public Maintainer() { this.map = new java.util.ConcurrentHashMap<Enum, List<Listener>>(); } public void addListener( Listener listener, Enum eventType ) { List<Listener> listeners; if( ( listeners = map.get( eventType ) ) == null ) { listeners = new java.util.concurrent.CopyOnWriteArrayList<Listener>(); map.put( eventType, listeners ); } listeners.add( listener ); } }
This code snippet is nothing but a bit improved listener pattern where each listener is telling what type of event it is interested in, and the provided method maintains a concurrent map of these relationships.
Initially, I wanted this method to be called via my own annotation framework, but bumped into a brick wall of various annotation limitations (e.g. you can’t have java.lang.Enum as annotation param, also there’s a set of various classloader issues) therefore decided to use Spring.
Could anyone tell me how do I Spring_ify_ this? What I want to achive is:
1. Define Maintainer class as a Spring bean.
2. Make it so that all sorts of listeners would be able to register themselves to Maintainer via XML by using addListener method. Spring doc nor Google are very generous in examples.
Is there a way to achieve this easily?
What would be wrong with doing something like the following:
Defining a ‘Maintainer’ interface with the addListener(Listener, Enum) method.
Create a DefaultMaintainer class (as above) which implements Maintainer.
Then, in each Listener class, ‘inject’ the Maintainer interface (constructor injection might be a good choice). The listener can then register itself with the Maintainer.
other than that, I’m not 100% clear on exactly what your difficulty is with Spring at the moment! 🙂