gpt4 book ai didi

java - spring 4.2 应用程序事件使用 Spring MVC 触发两次,为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:17 28 4
gpt4 key购买 nike

我正在使用 Spring 4.2.0.BUILD-SNAPSHOT events ,出于某种我还没有弄清楚的原因,监听器在发布任何事件后触发两次,无论是从 ApplicationEvent 扩展还是任何任意事件,但是在运行测试用例时一切都按预期工作,现在想知道注释是怎么回事Spring MVC 上下文中的驱动事件

事件发布接口(interface)

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.

——史蒂芬·尼科尔

追踪org.springframework.context.event.EventListenerMethodProcessor一切都发生两次

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 触发两次,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28871403/

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