Почему Spring Modulith @ApplicationModuleListener не запускается, когда опубликовано событие, которое он слушает?JAVA

Программисты JAVA общаются здесь
Anonymous
Почему Spring Modulith @ApplicationModuleListener не запускается, когда опубликовано событие, которое он слушает?

Сообщение Anonymous »

Я пытаюсь построить фиктивного POC, но мне что -то упускаю, что я не могу понять. Проблема заключается в том, что метод, аннотированный @ApplicationModuleListener , не запускается.

Код: Выделить всё

package com.example.demo;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@SpringBootApplication
@EnableAsync
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

@RestController
class MyController {

@GetMapping
String home() {
return "Hello";
}
}

@Component
@RequiredArgsConstructor
class CustomFilter extends OncePerRequestFilter {

private @NonNull final ApplicationEventPublisher applicationEventPublisher;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
this.applicationEventPublisher.publishEvent(new MyApplicationEvent("HTTP_REQUEST" , request.getRequestURI()));
filterChain.doFilter(request, response);
}
}

@Service
class MyApplicationModuleListener {

@org.springframework.modulith.events.ApplicationModuleListener
void on(MyApplicationEvent event) {
System.out.println(event.message());
}
}

record MyApplicationEvent(String type, String message) {}
< /code>
Вот мои зависимости Maven: < /p>

org.springframework.boot
spring-boot-starter-parent
3.3.4


…


org.springframework.modulith
spring-modulith-starter-core



org.springframework.modulith
spring-modulith-starter-jdbc



org.postgresql
postgresql
runtime

…
Странная вещь в том, что слушатель работает, когда я говорил, чтобы воспроизводить события при запуске, настройка Spring.modulith.republish-out-in-events-on-restart = false .
Вот время завершения события. src = "https://i.sstatic.net/1ruxep3l.png"/>
Я выдвинул код здесь https://github.com/jgarbora/poc-spring-modulith-events.

Подробнее здесь: https://stackoverflow.com/questions/790 ... n-an-event

Вернуться в «JAVA»