简体   繁体   English

使用Spring XML配置实现观察者模式?

[英]Implement observer pattern with Spring XML config?

Suppose bean A is event publisher (observable) and beans B1 , B2 and B3 are event listeners (observers). 假设bean A是事件发布者(可观察到),而bean B1B2B3是事件监听器(观察者)。

All Bs implement some BEvenListener interface. 所有B都实现一些BEvenListener接口。

How to code observable interface in A ? 如何在A编写可观察接口的代码? I wish to code something like usual Java's addEventListener(BEventListener listener) . 我希望编写类似于通常的Java的addEventListener(BEventListener listener)

How to set all Bs to listen for A in Spring config? 如何在Spring配置中将所有B设置为侦听A Usual "setter" allows only one injection, right? 通常的“设定者”只能注射一次,对吗? So, how to configure "adder" in Spring? 那么,如何在Spring中配置“ adder”呢?

Spring provides ApplicationListener and ApplicationEventPublisherAware which allows to write beans which listen events, published onto context. Spring提供了ApplicationListenerApplicationEventPublisherAware ,允许编写侦听发布到上下文的事件的bean。 But this mechanism works without configuration, ie XML file does not say which bean listen to which. 但是这种机制无需配置即可工作,即XML文件不会说哪个bean侦听哪个bean。 Only types matter and all beans coded to listen some event will listen to it if present in context. 只有类型很重要,所有编码为侦听某个事件的bean都会在上下文中侦听该事件。 Ie events are context-wide. 即事件是上下文范围内的。

Is it possible to have directed event "channel" between publisher and listeners, configured in XML? 是否可以在发布者和侦听器之间使用XML配置定向事件“通道”?

UPDATE 更新

The only idea I got yet is to inject observable to filter events. 我唯一想到的就是注入可观察的事件过滤器。

So, classes will be 因此,课程将是

public class Observable implements ApplicationEventPublisherAware {

public static class Event extends ApplicationEvent {

    public Event(Object source) {
        super(source);
    }

}

private ApplicationEventPublisher applicationEventPublisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher value) {
    this.applicationEventPublisher = value;
}

public void somecode() {
    applicationEventPublisher.publishEvent(new Event(this));
}

}

and

public class Observer implements ApplicationListener<Observable.Event> {

private Observable observable;

public void setObservable(Observable value) {
    this.observable = value;
}

@Override
public void onApplicationEvent(Event event) {
    if( event.getSource() == observable ) {
        // process event
    }
}

}

and the config will be 和配置将是

<bean id="observable" class="tests.observer.Observable"/>

<bean id="observer" class="tests.observer.Observer">
    <property name="observable" ref="observable"/>
</bean>

This will allow set publisher explicitly. 这将允许显式设置发布者。

This is something I wish Spring would add to their XML configs, but as it stands you have to make use of the MethodInvokingFactoryBean if you want to avoid tying your code to Spring. 我希望Spring将这些添加到其XML配置中,但是就目前情况而言,如果要避免将代码绑定到Spring,则必须使用MethodInvokingFactoryBean

For the purposes of this example assume that the following method has been added your tests.observer.Observable class 出于本示例的目的,假定已将以下方法添加到tests.observer.Observable类中

void addObserver(Observer observer);

<bean id="observable" class="tests.observer.Observable"/>

<bean id="observer" class="tests.observer.Observer">
    <property name="observable" ref="observable"/>
</bean>

<bean id="addObserver" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="observable" />
    <property name="targetMethod" value="addObserver"/>
    <property name="arguments">
       <list>
          <ref bean="observer" />
       </list>
    </property>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM