i want create event notifier in repository layer can add listeners. using spring , wonder if okey way of doing it? or there better ways implement notifying/listeners in spring?
@repository public class jdbcrepository { private list<insertlistener> insertlisteners; public void insert(final someobject object) { // ommited code brewity (insertlistener listener : insertlisteners) { listener.notifiy(...); } } }
spring config xml
<bean id="jdbcrepository" class="mypackage.jdbcrepository"> <property> <bean ref="myrepositorylisteners" /> </property> </bean> <bean id="myrepositorylisteners" class="java.util.list"> <constructor-arg> <list> <ref bean="..." /> <ref bean="..." /> </list> </constructor-arg> </bean>
you can way
public class jdbcrepository { @autowired applicationcontext context; collection<insertlistener> listeners; @postconstruct public void init() { listeners = context.getbeansoftype(insertlistener.class).values(); } ...
context.xml
<context:annotation-config /> <bean id="l1" class="insertlistener" /> <bean id="l2" class="insertlistener" /> <bean id="repo" class="jdbcrepository" />
Comments
Post a Comment