iam*_*ddy 3 java spring spring-mvc
我正在使用Spring 4.2.0.BUILD-SNAPSHOT事件,由于某些原因我还没想到,监听器在发布任何事件后都会触发两次,无论是从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...");
}
}
Run Code Online (Sandbox Code Playgroud)
事件监听器
@EventListener
@Async
public void sendMailForSuggestedListing(Supplier<ListingResource> listingCreatedEvent) {
System.out.println("Event fired...");
}
Run Code Online (Sandbox Code Playgroud)
终点/切入点
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;
}
Run Code Online (Sandbox Code Playgroud)
结果:事件被解雇...事件被解雇...上面的事件..
我怀疑EventListener bean已经注册了两次或者其他什么.您可以启用org.springframework.context.event.EventListenerMethodProcessor来跟踪级别以检查此特定类会发生什么.
- StéphaneNicoll
追踪
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
Run Code Online (Sandbox Code Playgroud)
Java配置
@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
Run Code Online (Sandbox Code Playgroud)
我有同样的问题.对我来说......我注册了两次事件监听器,因为我在一个配置类中覆盖了我的整个包路径
@ComponentScan( basePackages = {"my.root.package"} )
@Configuration
public class MyAppConfig {
//...
}
Run Code Online (Sandbox Code Playgroud)
然后我在另一个配置类中手动添加了相同的eventListener bean,就像这样
@Configuration
public class SpringConfig {
...
// SomeEventListener was located @ my.root.package.event and had @Component annotation
@Bean
public SomeEventListener someEventListener() {
return new SomeEventListener();
}
...
}
Run Code Online (Sandbox Code Playgroud)
导致它注册两次.
归档时间: |
|
查看次数: |
3292 次 |
最近记录: |