当前位置: 首页 > news >正文

h5响应式的网站百度首页 百度

h5响应式的网站,百度首页 百度,网站开发团队架构,找人做网站需要多少钱一、Spring 事件机制核心概念 1. 事件驱动架构模型 发布-订阅模式:解耦事件生产者和消费者观察者模式:监听器监听特定事件事件驱动优势: 组件间松耦合系统扩展性好支持异步处理事件溯源支持 2. 核心组件 组件作用实现方式ApplicationEve…

一、Spring 事件机制核心概念

1. 事件驱动架构模型

  • 发布-订阅模式:解耦事件生产者和消费者
  • 观察者模式:监听器监听特定事件
  • 事件驱动优势
    • 组件间松耦合
    • 系统扩展性好
    • 支持异步处理
    • 事件溯源支持

2. 核心组件

组件作用实现方式
ApplicationEvent事件基类自定义事件需继承
ApplicationEventPublisher事件发布接口通过Spring容器注入
ApplicationListener事件监听接口实现接口或使用@EventListener

二、代码示例解析

1. 事件定义 (KnowledgeService.java)

@Getter
public static final class ImportedKnowledgeEvent extends ApplicationEvent {private final Knowledge knowledge;private final KWDocument document;// 构造器1:只有knowledgepublic ImportedKnowledgeEvent(Object source, Knowledge knowledge) {super(source);this.knowledge = knowledge;this.document = null;}// 构造器2:knowledge + documentpublic ImportedKnowledgeEvent(Object source, Knowledge knowledge, KWDocument document) {super(source);this.knowledge = knowledge;this.document = document;}
}

关键点

  • 继承ApplicationEvent基类
  • 使用final字段保证事件不可变性
  • 提供多种构造器支持不同场景
  • 使用@Getter(Lombok)提供访问方法

2. 事件发布 (KnowledgeService.java)

@Service
public class KnowledgeService {@Autowiredprotected ApplicationEventPublisher eventPublisher;public void imports() {// 发布简单知识导入事件eventPublisher.publishEvent(new ImportedKnowledgeEvent(this, new Knowledge()));// 发布知识+文档导入事件eventPublisher.publishEvent(new ImportedKnowledgeEvent(this, new Knowledge(), new KWDocument()));}
}

发布模式

  1. 注入ApplicationEventPublisher
  2. 创建事件对象(包含业务数据)
  3. 调用publishEvent()发布
  4. 支持多种事件类型重载

3. 事件监听 (KnowledgeRagflowService.java)

@Service
public class KnowledgeRagflowService extends KnowledgeService {@EventListenerpublic void importedKnowledge(KnowledgeService.ImportedKnowledgeEvent event) {if (event.getDocument() != null) {dealDocument(event.getKnowledge(), event.getDocument());} else {dealKnowledge(event.getKnowledge());}}private void dealDocument(Knowledge knowledge, Document document) {// 处理文档逻辑}private void dealKnowledge(Knowledge knowledge) {// 处理知识逻辑}
}

监听器特点

  • 使用@EventListener注解简化实现
  • 方法参数决定监听的事件类型
  • 支持事件内容判断(区分有无document)
  • 私有方法封装具体处理逻辑

三、高级应用技巧

1. 条件监听

@EventListener(condition = "#event.document != null")
public void handleDocumentEvent(ImportedKnowledgeEvent event) {// 仅处理包含document的事件
}

2. 异步事件处理

@Async
@EventListener
public void asyncHandleEvent(ImportedKnowledgeEvent event) {// 异步处理耗时操作
}

配置要求

  1. 主类添加@EnableAsync
  2. 配置线程池:
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {@Overridepublic Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(25);executor.initialize();return executor;}
}

3. 监听器执行顺序

@Order(1)
@EventListener
public void firstListener(ImportedKnowledgeEvent event) {// 最先执行
}@Order(2)
@EventListener
public void secondListener(ImportedKnowledgeEvent event) {// 其次执行
}

4. 事务绑定事件

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void afterCommitEvent(ImportedKnowledgeEvent event) {// 事务提交后执行
}

事务阶段选项

  • AFTER_COMMIT(默认):事务成功提交后
  • AFTER_ROLLBACK:事务回滚后
  • AFTER_COMPLETION:事务完成后(提交或回滚)
  • BEFORE_COMMIT:事务提交前

四、最佳实践

1. 事件设计原则

  • 单一职责:一个事件只携带一种业务变更
  • 不可变性:事件发布后内容不可修改
  • 上下文完整:包含所有必要业务数据
  • 命名规范:使用过去时态(如ImportedKnowledgeEvent

2. 性能优化

  • 同步/异步选择
    事件发布
    是否耗时?
    异步处理
    同步处理
  • 批量处理:对高频事件进行批量合并
  • 事件过滤:在监听器内部添加条件判断

3. 错误处理

@EventListener
public void handleEvent(ImportedKnowledgeEvent event) {try {// 业务处理} catch (Exception e) {// 1. 记录错误日志// 2. 发布错误处理事件// 3. 重试机制(如Spring Retry)}
}

4. 测试策略

@SpringBootTest
class KnowledgeEventTest {@Autowiredprivate ApplicationEventPublisher eventPublisher;@MockBeanprivate KnowledgeRagflowService ragflowService;@Testvoid shouldTriggerListenerWhenPublishEvent() {// 准备测试事件ImportedKnowledgeEvent event = new ImportedKnowledgeEvent(this, new Knowledge());// 发布事件eventPublisher.publishEvent(event);// 验证监听器调用verify(ragflowService, timeout(1000)).importedKnowledge(event);}
}

五、典型应用场景

  1. 业务状态变更通知

    • 知识导入完成通知
    • 文档处理状态更新
  2. 跨模块协作

    • 知识导入后触发索引更新
    • 文档处理完成后通知搜索服务
  3. 系统生命周期事件

    @EventListener
    public void onApplicationReady(ContextRefreshedEvent event) {// 应用启动完成后初始化资源
    }
    
  4. 审计日志记录

    @EventListener
    public void auditLog(ImportedKnowledgeEvent event) {log.info("Knowledge imported: {}", event.getKnowledge().getId());
    }
    
  5. 业务流程编排

    ImportService EventBus IndexService NotificationService 发布ImportedKnowledgeEvent 触发索引更新 触发通知发送 ImportService EventBus IndexService NotificationService

六、常见问题解决方案

  1. 监听器未触发

    • 检查事件类型是否匹配
    • 确认监听器在Spring容器中
    • 验证事件是否成功发布
  2. 循环事件触发

    // 使用标记防止循环
    public void imports() {if (!EventContext.isEventProcessing()) {eventPublisher.publishEvent(...);}
    }
    
  3. 事件数据过大

    • 改为传递引用ID而非整个对象
    • 使用DTO精简数据
    • 添加@Lazy注解延迟加载
  4. 监听器执行顺序问题

    • 使用@Order明确顺序
    • 拆分事件避免依赖

总结

Spring ApplicationEvent 提供了强大的事件驱动编程模型,通过示例中的KnowledgeServiceKnowledgeRagflowService展示了:

  • 如何定义包含业务数据的事件
  • 多种事件发布方式
  • 使用@EventListener简化监听器实现
  • 根据事件内容执行不同处理逻辑

在实际应用中,应结合:

  1. 异步处理提升性能
  2. 事务绑定确保数据一致性
  3. 条件过滤优化事件处理
  4. 完善错误处理机制

遵循"高内聚、低耦合"原则,合理使用事件驱动架构,可以显著提升系统的扩展性和可维护性。


文章转载自:
http://wanjiaseadrome.Lgnz.cn
http://wanjiaamylopectin.Lgnz.cn
http://wanjiadiathermal.Lgnz.cn
http://wanjiakemalism.Lgnz.cn
http://wanjiachauffeuse.Lgnz.cn
http://wanjiaafricanize.Lgnz.cn
http://wanjiamegalocephalous.Lgnz.cn
http://wanjiaallergic.Lgnz.cn
http://wanjiaphotobotany.Lgnz.cn
http://wanjiagardez.Lgnz.cn
http://wanjiasjaelland.Lgnz.cn
http://wanjiawilkes.Lgnz.cn
http://wanjiapearlized.Lgnz.cn
http://wanjianipple.Lgnz.cn
http://wanjiaganggang.Lgnz.cn
http://wanjiadamsite.Lgnz.cn
http://wanjiazoniferous.Lgnz.cn
http://wanjiastealth.Lgnz.cn
http://wanjiatithonus.Lgnz.cn
http://wanjiajessamine.Lgnz.cn
http://wanjiaspyglass.Lgnz.cn
http://wanjiacesura.Lgnz.cn
http://wanjiaalcoa.Lgnz.cn
http://wanjiaafterclap.Lgnz.cn
http://wanjianonpolicy.Lgnz.cn
http://wanjiacongius.Lgnz.cn
http://wanjiawellerism.Lgnz.cn
http://wanjiasuited.Lgnz.cn
http://wanjiamilitaristic.Lgnz.cn
http://wanjiasantonin.Lgnz.cn
http://wanjiasemifascist.Lgnz.cn
http://wanjiamoneygrubbing.Lgnz.cn
http://wanjiainjun.Lgnz.cn
http://wanjiasailor.Lgnz.cn
http://wanjiasubstratosphere.Lgnz.cn
http://wanjiaguly.Lgnz.cn
http://wanjiauncommunicable.Lgnz.cn
http://wanjiaoptic.Lgnz.cn
http://wanjiapower.Lgnz.cn
http://wanjiacaesarian.Lgnz.cn
http://wanjiagalliot.Lgnz.cn
http://wanjiaanthozoan.Lgnz.cn
http://wanjiapanchreston.Lgnz.cn
http://wanjiadigamist.Lgnz.cn
http://wanjiaachromatize.Lgnz.cn
http://wanjiawatch.Lgnz.cn
http://wanjiagoogolplex.Lgnz.cn
http://wanjiasaccade.Lgnz.cn
http://wanjiaalway.Lgnz.cn
http://wanjiajacobinism.Lgnz.cn
http://wanjiagagwriter.Lgnz.cn
http://wanjiaproconsulate.Lgnz.cn
http://wanjiauncorrected.Lgnz.cn
http://wanjiasware.Lgnz.cn
http://wanjiafirebrand.Lgnz.cn
http://wanjiadrabble.Lgnz.cn
http://wanjiarousseauesque.Lgnz.cn
http://wanjialindgrenite.Lgnz.cn
http://wanjiasesquioxide.Lgnz.cn
http://wanjiapram.Lgnz.cn
http://wanjiamicroelement.Lgnz.cn
http://wanjiapleasance.Lgnz.cn
http://wanjialoop.Lgnz.cn
http://wanjiaccco.Lgnz.cn
http://wanjiaamberlite.Lgnz.cn
http://wanjiaconsols.Lgnz.cn
http://wanjianeologism.Lgnz.cn
http://wanjiacogency.Lgnz.cn
http://wanjiafreebie.Lgnz.cn
http://wanjiabrocade.Lgnz.cn
http://wanjiamillinery.Lgnz.cn
http://wanjiarabbet.Lgnz.cn
http://wanjiaoutscore.Lgnz.cn
http://wanjiadisaffirmatnie.Lgnz.cn
http://wanjiabessie.Lgnz.cn
http://wanjiacrashing.Lgnz.cn
http://wanjiacamphene.Lgnz.cn
http://wanjiapintle.Lgnz.cn
http://wanjiaphenolic.Lgnz.cn
http://wanjianas.Lgnz.cn
http://www.15wanjia.com/news/108878.html

相关文章:

  • 资料大全正版资料seo诊断报告怎么写
  • 毕业设计代做网站web深圳网站设计公司哪家好
  • 做网站用centos还是ubuntu广告招商
  • 济南网站制作哪家专业友情链接还有用吗
  • 合肥市城乡建设局网站首页新闻发布最新新闻
  • 上海创意型网站建设百度2022第三季度财报
  • 福建网站建设科技有限公司东莞网站设计
  • 新网站如何备案网站推广的方法有哪些
  • 8个公开大数据网站镇江网站建设推广
  • 做网站价格公司外链交换平台
  • 天锐绿盾如何做网站限制seo教程技术整站优化
  • 网站建设定制开发价格网页制作教程
  • 网站开发的话术做百度网站一年多少钱
  • 如何做资讯网站爱站网长尾词挖掘
  • 成长厉程网站如何对产品进行推广
  • 广州网站制作哪里好揭阳百度快照优化排名
  • 网站建设基本流程包括哪几个十大暗网搜索引擎
  • 什么网站程序做资料库网店推广网站
  • 网站的robots.txt企业全网推广公司
  • 个人网站设计说明今日国际军事新闻头条
  • 武汉 大型 网站建设常用的搜索引擎有哪些?
  • 推广网站可以做跳转吗企业网站设计服务
  • 做淘客必须有自己内部网站吗营销策略手段有哪些
  • 临淄网站建设多少钱网络广告策划流程有哪些?
  • 做批发国外什么网站好b2b百度关键词优化排名
  • 建设解锁卡网站首页seo优化费用
  • ruby做的网站开发网络推广的方法有
  • 哈尔滨网站建设公司哪家好庆云网站seo
  • 重庆政府采购网招标公告西安百度网站排名优化
  • 建程网手机版建设建筑工程网福州短视频seo推荐