- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个下一个 customEventPublisher 类:
@Component
public class CustomEventPublisher {
@Autowired
private static ApplicationEventPublisher applicationEventPublisher;
public static void doStuffAndPublishAnEvent(User user) {
OnPostUserDataEvent onPostUserDataEvent = new OnPostUserDataEvent(user, REST_URL);
applicationEventPublisher.publishEvent(onPostUserDataEvent);
}
}
服务中调用的publishEvent方法:
public User create(User user) {
CustomEventPublisher.doStuffAndPublishAnEvent(user);
return repository.save(user);
}
配置:
<context:component-scan base-package="example.service"/>
<context:component-scan base-package="example.util.emailUtil"/>
事件实现于:
public class OnPostUserDataEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
private String appUrl;
private User user;
public OnPostUserDataEvent(User user, String appUrl) {
super(user);
this.user = user;
this.appUrl = appUrl;
}
}
我捕获 NullPointedException 的原因是:
applicationEventPublisher.publishEvent(onPostUserDataEvent)
也许ApplicationEventPublisher不在@Autowired中,但我不明白为什么。
最佳答案
问题是你正在与 AOP 作斗争。 doStuffAndPublishAnEvent
方法是静态的,因此不会在可以注入(inject) ApplicationEventPublisher
的实例上调用它。
要克服这个问题,您必须将 doStuffAndPublishAnEvent
设置为非静态,并将 CustomEventPublisher
注入(inject)到您的服务中,以便您可以通过实例调用代理上的方法。
关于java - ApplicationEventPublisher NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60399499/
我有一个下一个 customEventPublisher 类: @Component public class CustomEventPublisher { @Autowired private st
我需要使用事件驱动机制。使用ApplicationEventPublisher。我想在到达监听此事件的端点之前为消息设置延迟。有这样的办法吗?找到https://docs.spring.io/spri
我想使用 Spring Web Flow 步骤中的 ApplicationEventPublisher#publishEvent() 发布应用程序事件。发布者通常是应用程序上下文,但除非有必要(特别是
我正在尝试构建一个 DiscoveryClient,我希望它在路线发生更改时触发一个事件。我正在使用 publisher.publishEvent(new InstanceRegisteredEven
我有一个正在发布事件的 Controller @RestController public class Controller { @Autowired private Applicat
我有一个这样定义的类: public class MyClass extends SimpleChannelInboundHandler> implements ApplicationEventPub
我看到有很多实现,但是如何在不使用 Autowiring 和使用 xml 配置的情况下使用默认实现? 最佳答案 有多种选择,您可以使用注释、实现和接口(interface)或在 xml 或 java
我是一名优秀的程序员,十分优秀!