Files
springboot-demo/src/main/java/com/example/demo/event/UserEventPublisher.java
2026-03-19 13:53:49 +08:00

31 lines
1.1 KiB
Java

package com.example.demo.event;
import com.example.demo.model.UserEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
public class UserEventPublisher {
@Autowired
private ApplicationEventPublisher eventPublisher;
public void publishEvent(UserEvent event) {
System.out.println("[EventPublisher] Publish event " + event.getType() + " for " + event.getUserName());
eventPublisher.publishEvent(event);
}
public void publishUserEvent(Long userId, String userName, UserEvent.Type type, String detail) {
publishEvent(new UserEvent(type, userId, userName, detail));
}
public void publishUserCreated(Long userId, String userName) {
publishUserEvent(userId, userName, UserEvent.Type.CREATED, "User created successfully");
}
public void publishUserLogin(Long userId, String userName) {
publishUserEvent(userId, userName, UserEvent.Type.LOGIN, "User login succeeded");
}
}