在项目中如果按照多个模块开发的话就会涉及到数据之间的关联
比如设备基础模块的数据会提供给其他模块(门禁,考勤等)使用;如果公共模块的数据发生改变就要通知其他模块知道,比如,删除,修改等
这个时候就需要业务解耦;实现的方式有很多种,springboot有自带的事件通知;
当事件发生改变的时候,就会接收到消息:
继承事件:
@Getter
public class DeviceEvent extends ApplicationEvent {
    private Integer id;
    private String tableName;
    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public DeviceEvent(Object source, Integer id, String tableName) {
        super(source);
        this.id = id;
        this.tableName = tableName;
    }
}注册事件监听器,处理事件发生:
@Configuration
public class DeviceListener {
    @Autowired
    private DoorDeviceInfoMapper doorDeviceInfoMapper;
    @EventListener
    @Async //异步处理防止影响主流程
    public void onApplicationEvent(DeviceEvent event) {
        LambdaQueryWrapper<DoorDeviceInfo> queryWrapper = new QueryWrapper<DoorDeviceInfo>().lambda();
        queryWrapper.eq(DoorDeviceInfo::getDeviceId, event.getId())
                .eq(DoorDeviceInfo::getTableName, event.getTableName());
        doorDeviceInfoMapper.delete(queryWrapper);
    }
}在业务处将事件发送出去
    @Autowired
    private ApplicationContext applicationContext;
    //事件发生的方法
     DeviceEvent deviceEvent = new DeviceEvent(this, id, tableName);
     applicationContext.publishEvent(deviceEvent);