gpt4 book ai didi

java - Spring 3.1.2 : ApplicationEventPublisher not being set

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:56 24 4
gpt4 key购买 nike

我有一个这样定义的类:

public class MyClass extends SimpleChannelInboundHandler<DataFrame<ByteBuf>> implements ApplicationEventPublisherAware

(SimpleChannelInboundHandler 是一个 io.netty 类。)

然后,在我的 xml 文件中,我像这样定义 MyClass:

<bean id="MyClass" class="com.mypackage.MyClass" />

根据文档:

At configuration time, the Spring container will detect that EmailService implements ApplicationEventPublisherAware and will automatically call setApplicationEventPublisher().

但是当我运行这个时它是空的。

有什么想法吗?

谢谢

最佳答案

ApplicationEventPublisherAware 的常见使用模式看起来像这样:

package example;

import org.springframework.stereotype.*;
import org.springframework.context.*;

@Component
public class MyBean implements ApplicationEventPublisherAware {
ApplicationEventPublisher applicationEventPublisher;

public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
System.out.println("publisher: " + applicationEventPublisher);
this.applicationEventPublisher = applicationEventPublisher;
}

... (use applicationEventPublisher in methods)
}

你只需要确保bean确实通过组件扫描/配置/<bean>添加到上下文中标签,尝试将其注入(inject)到另一个 bean 中进行检查。

关于java - Spring 3.1.2 : ApplicationEventPublisher not being set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24291975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com