• @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.0modelVersion>
    <groupId>com.olivegroupId>
    <artifactId>olive-starterartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.7.1version>
    <relativePath/>
    parent>
    <name>olive-startername>
    <description>olive starter for Spring Bootdescription>
    <properties>
    <java.version>1.8java.version>
    properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starterartifactId>
    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 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 getRouters() {
    //TODO
    System.out.println("-------------DefaultRouterServiceImpl----------");
    return new ArrayList();
    }
    }

    配置默认的路由加载 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.0modelVersion>
    <groupId>com.olivegroupId>
    <artifactId>olive-gatewayartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.7.1version>
    <relativePath />
    parent>
    <name>olive-startername>
    <description>olive starter for Spring Bootdescription>
    <properties>
    <java.version>1.8java.version>
    properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starterartifactId>
    dependency>
    <dependency>
    <groupId>com.olivegroupId>
    <artifactId>olive-starterartifactId>
    <version>0.0.1-SNAPSHOTversion>
    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 getRouters() {
    System.out.println("-------CustomRouterServiceImpl------");
    return new ArrayList<>();
    }
    }

    再次启动项目

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

  • 相关阅读:
    【在凸多边形的图像中查找顶点】估计具有已知顶点数的像素化凸多边形角点研究(Matlab代码实现)
    【个人亲历】上海市人才引进落户最详细的流程记录说明
    循环神经网络(RNN)
    sql:SQL优化知识点记录(七)
    C/C+=内存管理
    FreeRTOS介绍 和 将FreeRTOS移植到STM32F103C8T6
    你还不知道吗?新一代隐私保护技术已被应用到笔记本电脑中!
    人工智能中的文本分类:技术突破与实战指导
    如何快速重置模型原点
    【CUDA OUT OF MEMORY】【Pytorch】计算图与CUDA OOM
  • 原文地址:https://www.cnblogs.com/happyhuangjinjin/p/16485437.html