@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean

简介: @ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
  1. 自定义一个简单 spring-boot 组件
  • 创建 olive-starter 项目

对应的 pom.xml文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.olive</groupId>
  <artifactId>olive-starter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.1</version>
    <relativePath/>
  </parent>
  <name>olive-starter</name>
  <description>olive starter for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
</project>

注意这里不要引入 spring-boot 的插件,使用maven原生的,否则使用maven  install的时,其他工程无法引入

  • 定义一个加载路由的接口
package com.olive.service;
import java.util.List;
import com.olive.model.RouterDO;
public interface RouterService {
  public List<RouterDO> getRouters();
}

路由的实体类 DouterDO 与实现类如下

DouterDO.java 类

package com.olive.model;
import java.io.Serializable;
public class RouterDO implements Serializable{
  private String id;
  private String routerName;
  //TODO 省略getter  setter
}

DefaultRouterServiceImpl.java 类,默认的路由加载策略

package com.olive.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.olive.model.RouterDO;
import com.olive.service.RouterService;
public class DefaultRouterServiceImpl implements RouterService{
  @Override
  public List<RouterDO> getRouters() {
    //TODO
     System.out.println("-------------DefaultRouterServiceImpl----------");
    return new ArrayList<RouterDO>();
  }
}
  • 配置默认的路由加载 Bean
package com.olive.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.olive.service.RouterService;
import com.olive.service.impl.DefaultRouterServiceImpl;
@Configuration
public class RouterConfig {
  @Bean
    @ConditionalOnMissingBean
    public RouterService getRouterService(){
        return new DefaultRouterServiceImpl();
    }
}

创建 MATE-INF 文件夹,并在该文件下创建 spring.factories 文件;spring.factories 文件内容如下


org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.olive.config.RouterConfig

整体项目结构如下:



2. 创建另外一个项目 olive-gateway,引用 olive-starter 工程

  • olive-gateway 的 pom.xml 文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.olive</groupId>
  <artifactId>olive-gateway</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.1</version>
    <relativePath />
  </parent>
  <name>olive-starter</name>
  <description>olive starter for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>com.olive</groupId>
      <artifactId>olive-starter</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
  • 创建 springboot 启动引导类
package com.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.olive.service.RouterService;
@SpringBootApplication
public class GwApplication {
  public static void main(String[] args) {
    ApplicationContext ac = SpringApplication.run(GwApplication.class, args);
    RouterService routerService = ac.getBean(RouterService.class);
    routerService.getRouters();
  }
}

启动项目

观察日志,使用默认的路由加载类 DefaultRouterServiceImpl

  • 再自定义一个路由加载类实现 RouterService 接口
package com.gateway.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.olive.model.RouterDO;
import com.olive.service.RouterService;
@Service
public class CustomRouterServiceImpl implements RouterService {
  @Override
  public List<RouterDO> getRouters() {
    System.out.println("-------CustomRouterServiceImpl------");
    return new ArrayList<>();
  }
}

再次启动项目

观察日志,已经覆盖默认的路由加载类 DefaultRouterServiceImpl

相关文章
|
Nacos
服务器部署 Nacos 获取不到配置浏览器可以访问
[Nacos Config] config[dataId=ruoyi-auth.yml,group=DEFAULT_GROUP] is empty、Nacos无法注册、gRPC、端口偏移
950 0
服务器部署 Nacos 获取不到配置浏览器可以访问
hutool验证是否为中文字、英文字母、数字和下划线
hutool验证是否为中文字、英文字母、数字和下划线
|
SpringCloudAlibaba 安全 Java
SpringCloud版本升级后bootstrap.yml配置不生效
SpringCloud版本升级后bootstrap.yml配置不生效
3221 1
|
人工智能 缓存 供应链
森马如何用阿里云 AI 网关,轻松实现“AI+业务”高效落地
森马快速实现 AI 转型,通过阿里云 AI 网关(即 Higress 企业版)及注册配置中心 Nacos3.0 实现了多模型多 MCP server 统一接入统一管理统一配置,将存量服务一键转换为 MCP server,使 AI 与生产业务相结合,综合提效 30%。
1751 91
Idea在debug时打上断点没有用 Skipped breakpoint at ... because it happened inside debugger evaluation
Idea在debug时打上断点没有用 Skipped breakpoint at ... because it happened inside debugger evaluation
3829 0
|
IDE API 开发工具
让 GitHub 个人主页更专业:用 WakaTime 实现动态代码统计
这篇文章介绍了如何使用 WakaTime 为 GitHub 个人主页添加动态代码统计,展示编码活跃度,提升专业性。通过注册账号、安装 IDE 插件、配置 GitHub 仓库和使用工作流自动更新 README 文件,实现对多种编程语言的详细统计。
1208 1
让 GitHub 个人主页更专业:用 WakaTime 实现动态代码统计
|
容灾 Java 测试技术
低成本、高稳定性 |满帮集团 Eureka 和 ZooKeeper 的上云实践
充分了解和利用云的能力,能够让满帮技术团队从底层的持续投入中解脱出来,聚焦更上层的系统稳定性和工程效率,从架构层面实现更高的 ROI。
68768 131
|
存储 运维 Kubernetes
Java启动参数JVM_OPTS="-Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError"
本文介绍了Java虚拟机(JVM)常用启动参数配置,包括设置初始堆内存(-Xms512m)、最大堆内存(-Xmx1024m)及内存溢出时生成堆转储文件(-XX:+HeapDumpOnOutOfMemoryError),用于性能调优与故障排查。
1683 0
|
负载均衡 网络协议 前端开发
一文快速上手 Nacos 注册中心+配置中心!
一文快速上手 Nacos 注册中心+配置中心!
10444 0
|
Java 调度 数据库
SpringBoot整合XXL-JOB【05】- 任务分片
在实际业务中,批量定时任务可能因上一批任务未完成而影响业务。为解决此问题,本文介绍如何使用Xxl-job对批量任务进行分片处理,通过分片广播形式调度集群机器并行执行任务,大幅提升执行效率。具体步骤包括环境准备、添加依赖和配置、声明实体类与查询类,以及改造业务逻辑实现分片查询。测试结果显示,分片处理将两千条数据的执行时间从30秒缩短至15秒,性能提升显著。
2857 13
SpringBoot整合XXL-JOB【05】-  任务分片