首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在自定义CacheEventListener中,带有Ehcache 3.x - @Autowired服务的SpringBoot2.x为空

在自定义CacheEventListener中,带有Ehcache 3.x - @Autowired服务的SpringBoot2.x为空
EN

Stack Overflow用户
提问于 2020-12-15 22:42:45
回答 1查看 410关注 0票数 0

我使用Ehcache作为缓冲区,为连接到WebSocket(Spring)的所有客户端传递数据。

CacheEventListener实现:

代码语言:javascript
复制
public class CacheListener implements CacheEventListener<ArrayList, MeasurementPoint> {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate; //<-- This is null at runtime

    @Override
    public void onEvent(CacheEvent<? extends ArrayList, ? extends MeasurementPoint> cacheEvent) {
        simpMessagingTemplate.convertAndSend("/topic/measurementsPoints", cacheEvent.getNewValue());
    }
}

Ehcache配置xml:

代码语言:javascript
复制
config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
            http://www.ehcache.org/v3
            http://www.ehcache.org/schema/ehcache-core-3.7.xsd">
    <!-- Default cache template -->
    <cache-template name="default">
        <resources>
            <heap>1000</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache-template>

    <cache alias="measurementsCache" uses-template="default">
        <key-type>java.util.ArrayList</key-type>
        <value-type>br.com.petrobras.risersupervisory.model.MeasurementPoint</value-type>
        <listeners>
            <listener>
                <class>br.com.petrobras.risersupervisory.framework.CacheListener</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>UPDATED</events-to-fire-on>
            </listener>
        </listeners>
    </cache>
</config>

我已经尝试了一些教程,但是所有这些教程都在CacheEventListener实现中使用了日志库或CacheEventListener。

有没有办法在CacheEventListener中自动创建@Service(Spring)?这种实施是否错误?我找不到一个人在CacheEventListener中使用@Autowired的例子。

Obs:不只是SimpMessagingTemplate在运行时是空的。我也尝试过自定义类,结果是一样的。

Obs2:在搜索了弹簧引导日志之后,我相信CacheEventListener是在春季引导完成加载之前绑定到缓存的。不知道这是不是问题所在。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65314758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档