我有以下弹簧架豆。这个想法是它保留某物的一个实例,并在某物发生变化时触发一个更改事件(同时提供旧值和新值):
public class Holder<T> implements ApplicationEventPublisherAware {
private final Class<T> payloadType; //because java generics are incomplete
private T curr; //latest value being held
private ApplicationEventPublisher publisher; //spring context ref
public Holder(Class<T> payloadType) {
this.payloadType = payloadType;
}
public void change() throws Exception {
T prev = curr;
curr = payloadType.newInstance();
publisher.publishEvent(new ContentsChangedEvent<>(prev, curr));
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个对这些事件做出反应的监听器类:
@Named
public class Listener {
@EventListener
public void onChange(ContentsChangedEvent<Cat> event) {
//never gets called
}
@EventListener
public void onErasedChange(ContentsChangedEvent<?> event) {
//does get called
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是通用(正确)事件侦听器方法永远不会被调用(获取 的那个Event<Cat>),只有被删除的方法。
为了完整起见,这里是 spring context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="net.radai"/>
<bean id="holder" class="net.radai.Holder">
<constructor-arg type="java.lang.Class" value="net.radai.Cat"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我也把整件事都放在了 github 上 - https://github.com/radii-rosenblatt/spring-events。它是一个 Maven 项目——你构建它,你会得到一个失败的测试。
我究竟做错了什么?
注意:在实际用例中,我希望有多个这样的持有者并强制所有侦听器以不可接受的方式丢弃与他们无关的事件
如果您在通用事件类中实现 org.springframework.core.ResolvableTypeProvider ,则侦听器将解析它。举个例子:
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
public class EventBase<T> {
public T payload;
}
public class AppEvent<T> extends EventBase<T> implements ResolvableTypeProvider {
@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(
getClass(),
ResolvableType.forInstance(this.payload)
);
}
}
@Component
public class EventHandler {
@EventListener
public void handleString(AppEvent<String> event) {
System.out.println("Event[" + event.getClass().getSimpleName() + "], received:" + event);
}
@EventListener
public void handleLong(AppEvent<Long> event) {
System.out.println("Event[" + event.getClass().getSimpleName() + "], received:" + event);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2568 次 |
| 最近记录: |