@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
标签:boot,springframework,olive,Bean,ConditionalOnMissingBean,组件,import,com,starter 来源: https://www.cnblogs.com/happyhuangjinjin/p/16485437.html