Answer the question
In order to leave comments, you need to log in
Spring Custom Events | why is ApplicationListener working but @EventListener not working?
There is this abstract code:
package com.test.testTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class TestTestApplication {
public static void main(String[] args) {
SpringApplication.run(TestTestApplication.class, args);
}
@Autowired
MyService1 service1;
@PostConstruct
void init() {
service1.publish1();
}
}
package com.test.testTest;
import org.springframework.context.ApplicationEvent;
public class MyEvent1 extends ApplicationEvent {
public MyEvent1(Object source) {
super(source);
}
}
package com.test.testTest;
import org.springframework.context.ApplicationEvent;
public class MyEvent2 extends ApplicationEvent {
public MyEvent2(Object source) {
super(source);
}
}
package com.test.testTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class MyService1 {
@Autowired
ApplicationEventPublisher publisher;
public void publish1(){
log.info("pre publishEvent1");
publisher.publishEvent(new MyEvent1(this));
log.info("post publishEvent1");
}
public void publish2(){
log.info("pre publishEvent1");
publisher.publishEvent(new MyEvent2(this));
log.info("post publishEvent1");
}
}
package com.test.testTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class MyService2 {
@EventListener
public void handleMyEvent1(MyEvent1 event) {
log.info("receive event1 {} ", event);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@EventListener
public void handleMyEvent2(MyEvent2 event) {
log.info("receive event2 {} ", event);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>testTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testTest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@Service
@Slf4j
public class MyService2 implements ApplicationListener<MyEvent1> {
@Override
public void onApplicationEvent(MyEvent1 event) {
log.info("receive event1 {} ", event);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
In the main code, I changed the starter of the whole movement and began to rob:
@PostConstruct
void init() {
service1.publish1();
}
@EventListener
public void onContextStart(ContextRefreshedEvent event) {
service1.publish1();
}
If my memory serves me right, it fires right after the beans are created, but not yet initialized, but it
will work at the very end after the beans are initialized (and can even be called several times after the application starts if the context has changed) (ContextRefreshedEvent.class)
Accordingly, in the first In this case, a message can be sent, but the listener for this message has not yet been initialized, so it is not processed.
But this is just a guess.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question