Spring相关的实现
BusinessProcessEvent.java
package com.example.demo.event;
import org.springframework.context.ApplicationEvent;
public class BusinessProcessEvent extends ApplicationEvent {
private String instanceId;
private String operationType;
public BusinessProcessEvent(Object source, String instanceId, String operationType) {
super(source);
this.instanceId = instanceId;
this.operationType = operationType;
}
public String getInstanceId() {
return instanceId;
}
public String getOperationType() {
return operationType;
}
}
BusinessProcessEventLogger.java
package com.example.demo.listener;
import org.springframework.context.ApplicationListener;
public class BusinessProcessEventLogger implements ApplicationListener<BusinessProcessEvent> {
@Override
public void onApplicationEvent(BusinessProcessEvent event) {
String instanceId = event.getInstanceId();
String operationType = event.getOperationType();
// 记录日志
System.out.println("记录日志: Instance ID " + instanceId + "进行了" + operationType + "操作");
// 在此处添加实际的日志记录逻辑,如将日志写入数据库或文件
}
}
BusinessOperationService.java
package com.example.demo.service;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class BusinessOperationService implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public void executeBusinessOperation(String instanceId, String operationType) {
// 执行业务操作...
// 发布业务事件
publisher.publishEvent(new BusinessProcessEvent(this, instanceId, operationType));
}
}
AppConfig.java
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public BusinessProcessEventLogger businessProcessEventLogger() {
return new BusinessProcessEventLogger();
}
@Bean
public BusinessOperationService businessOperationService() {
return new BusinessOperationService();
}
}
DemoApplication.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.service.BusinessOperationService;
@SpringBootApplication
public class DemoApplication {
@Autowired
private BusinessOperationService businessOperationService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostConstruct
public void runAfterStartup() {
businessOperationService.executeBusinessOperation("exampleInstanceId", "新增操作");
businessOperationService.executeBusinessOperation("anotherInstanceId", "删除操作");
}
}
在上述代码中,DemoApplication 启动类会在Spring Boot应用启动后自动执行 runAfterStartup 方法,进而触发业务操作和日志记录。BusinessProcessEventLogger 作为监听器已经在Spring容器中注册并通过 @Bean 注解声明,因此在业务事件发生时会自动执行其 onApplicationEvent 方法。
在这段代码示例中,每个类扮演不同的角色,服务于整个事件驱动的业务日志记录流程。下面是每个类的实际作用:
BusinessProcessEvent.java
这个类代表了一个自定义的应用程序事件,继承自ApplicationEvent。每当业务操作(如新增或删除)完成时,都会发布一个此类实例。它包含了事件相关的数据成员,如instanceId和operationType,以便在事件处理时提供必要的信息。
BusinessProcessEventLogger.java
这个类实现了ApplicationListener
BusinessOperationService.java
此类模拟了业务操作的执行者,它实现了ApplicationEventPublisherAware接口,意味着它可以注入ApplicationEventPublisher对象。在实际的业务方法executeBusinessOperation中,执行完操作后,它会发布一个BusinessProcessEvent事件,通知所有订阅了此事件的监听器(如BusinessProcessEventLogger)。
AppConfig.java
这是一个Spring配置类,通过@Configuration注解表明这是一个Spring容器的配置源。在这个配置类中,我们定义了两个@Bean方法,分别用来创建并注册BusinessProcessEventLogger和BusinessOperationService到Spring IoC容器中。这样,Spring框架就能管理这两个类的生命周期,并确保它们之间的依赖关系得到满足。
DemoApplication.java
这是Spring Boot应用的入口点,通过@SpringBootApplication注解启用自动配置和组件扫描。在main方法中启动Spring Boot应用,并在runAfterStartup方法上使用@PostConstruct注解,确保在应用初始化完成后自动执行一些初始操作。在这里,我们在应用启动后立即执行两次businessOperationService的executeBusinessOperation方法,触发业务操作和相应的日志记录。由于BusinessOperationService已经通过AppConfig配置类中的@Bean方法注入到了Spring容器,所以这里的@Autowired注解可以成功注入该服务实例。
Java方式的实现
以下是一个基于Java的简单观察者模式实现,用于记录业务日志。首先,我们定义两个接口:Subject(主题)和Observer(观察者),然后创建具体实现类并应用到您的业务场景中。
import java.util.ArrayList;
import java.util.List;
// 定义Subject接口
interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String instanceId, String operationType);
}
// 定义Observer接口
interface Observer {
void update(String instanceId, String operationType);
}
// 业务逻辑类,充当Subject角色
class BusinessOperation implements Subject {
private List<Observer> observers = new ArrayList<>();
private String instanceId;
private String operationType;
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
// 执行业务方法,并在完成后通知所有观察者
public void executeBusinessOperation(String instanceId, String operationType) {
this.instanceId = instanceId;
this.operationType = operationType;
// 假设这里执行了具体的新增或删除操作...
// ...
// 操作成功,通知观察者
notifyObservers(instanceId, operationType);
}
@Override
public void notifyObservers(String instanceId, String operationType) {
for (Observer observer : observers) {
observer.update(instanceId, operationType);
}
}
}
// 日志记录类,充当Observer角色
class BusinessLogObserver implements Observer {
@Override
public void update(String instanceId, String operationType) {
System.out.println("记录日志: Instance ID " + instanceId + "进行了" + operationType + "操作");
// 在这里实际实现日志记录功能,例如将日志写入文件或数据库
}
}
// 使用示例
public class Main {
public static void main(String[] args) {
BusinessOperation businessOperation = new BusinessOperation();
BusinessLogObserver logObserver = new BusinessLogObserver();
// 注册观察者
businessOperation.registerObserver(logObserver);
// 执行业务操作并触发日志记录
businessOperation.executeBusinessOperation("exampleInstanceId", "新增操作");
businessOperation.executeBusinessOperation("anotherInstanceId", "删除操作");
}
}
在这个例子中,BusinessOperation 类是业务方法的具体实现,它在执行完新增或删除操作后通过调用 notifyObservers 方法通知观察者。BusinessLogObserver 类负责接收通知并记录日志。当有新的业务操作执行时,系统会自动记录相应的业务日志。
标签:String,通义,void,观察者,instanceId,operationType,日志,public,千问 From: https://www.cnblogs.com/xxsdnol/p/18079918