java – spring 4.2应用程序事件用Spring MVC触发两次,为什么?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – spring 4.2应用程序事件用Spring MVC触发两次,为什么?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3529字,纯文字阅读大概需要6分钟。
内容图文
![java – spring 4.2应用程序事件用Spring MVC触发两次,为什么?](/upload/InfoBanner/zyjiaocheng/734/8fb9953eb6634074ad1b4f11ec41e32f.jpg)
我正在使用Spring 4.2.0.BUILD-SNAPSHOT events,由于某些原因我还没想到,监听器在发布任何事件后都会触发两次,无论是从ApplicationEvent还是任意事件扩展,但是在运行测试用例时一切都按预期工作,现在想知道是什么正在继续使用Spring MVC上下文中的注释驱动事件
事件发布界面
public interface ListingRegistrationService {
public void registerListing(ListingResource listing);
}
@Component
class ListingRegistrationServiceImpl implements ListingRegistrationService{
private final ApplicationEventPublisher publisher;
@Autowired
public ListingRegistrationServiceImpl(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@Override
public void registerListing(ListingResource listing) {
//process
publisher.publishEvent(new ListingCreatedEvent(listing));
System.out.println("Event above...");
}
}
事件监听器
@EventListener
@Async
public void sendMailForSuggestedListing(Supplier<ListingResource> listingCreatedEvent) {
System.out.println("Event fired...");
}
终点/切入点
public ResponseEntity<ResponseStatus> registerListing(@RequestBody @Valid ListingResource listing,BindingResult result) throws URISyntaxException {
ResponseEntity<ResponseStatus> response = null;
listingService.registerListing(listing); // publish the event
response = ResponseEntity.created(new URI(""));
return response;
}
结果:
事件被解雇……
事件被解雇……
以上活动..
I suspect indeed that the EventListener bean is registered twice or
something. You can enable
org.springframework.context.event.EventListenerMethodProcessor to
trace level to check what happens to this particular class.
– StéphaneNic??oll
跟踪
org.springframework.context.event.EventListenerMethodProcessorIt发生了两次事情
12:02:32,878 DEBUG ntext.event.EventListenerMethodProcessor: 138 - 1 @EventListener methods processed on bean 'mailServiceImpl': [public void com.service.MailServiceImpl.sendMailForSuggestedListing(com.service.events.CreationEvent)]
12:02:32,878 DEBUG ntext.event.EventListenerMethodProcessor: 138 - 1 @EventListener methods processed on bean 'mailServiceImpl': [public void com.service.MailServiceImpl.sendMailForSuggestedListing(com.service.events.CreationEvent)]
12:02:32,878 TRACE ntext.event.EventListenerMethodProcessor: 132 - No @EventListener annotations found on bean class: class com.service.MetaServiceImpl
12:02:32,878 TRACE ntext.event.EventListenerMethodProcessor: 132 - No @EventListener annotations found on bean class: class com.service.MetaServiceImpl
Java configuration
@Configuration
@ComponentScan(basePackages = {"com.**.domain",
"com.**.repositories", "com.**.service",
"com.**.security" })
@PropertySource(value = { "classpath:application.properties" })
public class ServiceConfig
Configuration
@EnableWebMvc
@EnableSwagger
@EnableSpringDataWebSupport
@EnableMongoRepositories("com.**.repositories")
@ComponentScan(basePackages = {"com.**.config","com.**.rest.controllers","com.**.rest.tokens"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Configuration
@EnableMongoRepositories("com.**.**.repositories")
public class MongoRepositoryConfig extends AbstractMongoConfiguration
解决方法:
我有同样的问题.对我来说……我注册了两次事件监听器,因为我在一个配置类中覆盖了我的整个包路径
@ComponentScan( basePackages = {"my.root.package"} )
@Configuration
public class MyAppConfig {
//...
}
然后我在另一个配置类中手动添加了相同的eventListener bean,就像这样
@Configuration
public class SpringConfig {
...
// SomeEventListener was located @ my.root.package.event and had @Component annotation
@Bean
public SomeEventListener someEventListener() {
return new SomeEventListener();
}
...
}
导致它注册两次.
内容总结
以上是互联网集市为您收集整理的java – spring 4.2应用程序事件用Spring MVC触发两次,为什么?全部内容,希望文章能够帮你解决java – spring 4.2应用程序事件用Spring MVC触发两次,为什么?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。