Reputation: 49097
I want to create a event notifier in my Repository layer so I can add listeners. I am using Spring and I wonder if this a okey way of doing it? Or are there better ways to implement notifying/listeners in Spring?
@Repository
public class JdbcRepository {
private List<InsertListener> insertListeners;
public void insert(final SomeObject object) {
// Ommited code for brewity
for (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>
Upvotes: 1
Views: 164
Reputation: 136042
You can do it this 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" />
Upvotes: 1
Reputation: 3775
Take a look for Guava events and some Spring support for it.
Upvotes: 0