@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

整体项目结构如下:

df490e000ac758ca2884707ab0788826.png

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();
  }
  
}

启动项目

a460ca363ea4eae447351bb7ef828e11.png

观察日志,使用默认的路由加载类 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<>();
  }
}

再次启动项目

fb312642b35a41f0160079c5c5e4140f.png

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

记得点「」和「在看」↓

爱你们

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值