当前位置:网站首页>@How can conditionalonmissingbean cover beans in third-party components
@How can conditionalonmissingbean cover beans in third-party components
2022-07-19 07:47:00 【Bug trendsetter】
Customize a simple spring-boot Components
establish olive-starter project
Corresponding pom.xml The documents are as follows
<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>Be careful not to introduce spring-boot Plug in for , Use maven Native , Otherwise use maven install When the , Other projects cannot be introduced
Define an interface for loading routes
package com.olive.service;
import java.util.List;
import com.olive.model.RouterDO;
public interface RouterService {
public List<RouterDO> getRouters();
} Entity class of route DouterDO And implementation classes are as follows
DouterDO.java class
package com.olive.model;
import java.io.Serializable;
public class RouterDO implements Serializable{
private String id;
private String routerName;
//TODO Omit getter setter
}DefaultRouterServiceImpl.java class , Default route loading policy
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>();
}
}Configure the default route loading 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();
}
}establish MATE-INF Folder , And create... Under this file spring.factories file ;spring.factories The contents of the document are as follows
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.olive.config.RouterConfig The overall project structure is as follows :

2. Create another project olive-gateway, quote olive-starter engineering
olive-gateway Of pom.xml The documents are as follows
<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>establish springboot Start the boot class
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();
}
} Start project

Watch the log , Use the default route loading class DefaultRouterServiceImpl
Then customize a route loading class implementation RouterService Interface
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<>();
}
} Restart the project

Watch the log , The default route loading class has been overridden DefaultRouterServiceImpl
Remember to point 「 Fabulous 」 and 「 Looking at 」↓
Love you
边栏推荐
猜你喜欢

Spark3.x source code compilation

Flink introduction to practice - phase II (illustrated runtime architecture)

Where is the future of HMI conceptual design?
![[operation rules] how to realize TSN system level test?](/img/c0/ea85b7a414fe505082e4a90bf2c5f8.png)
[operation rules] how to realize TSN system level test?

实操教程:CANoe在CAN总线测试中的应用

Read gptp in a quarter of an hour

Real time data warehouse - Design & Implementation of real-time data warehouse from 0 to 1 (sparkstreaming3.x)

js数组交集、差集和并集

MongoDB的下载、安装和使用

一文带你了解SOA接口测试
随机推荐
Jenkins如何设置邮箱自动发送邮件?
Coursera deep learning notes
实时数据仓库-从0到1实时数据仓库设计&实现(SparkStreaming3.x)
Spark introduction to proficient - external part (operation and maintenance and simple operation of standaone cluster)
60. Clear cache
Flink入门到实战-阶段三(DataStreamAPI)
Promote trust in the digital world
一刻钟读懂gPTP
Introduction to arm development environment
目标检测和边界框
Environment variables and folder placement location
Filter filter
Component emit Foundation
Prevent blackmail attacks through data encryption schemes
OpenSUSE install Netease cloud music (tumblefeed) (LEAP)
Review - 5703 Statistical Inference and Modeling
FreeBSD 12 installing RPM packages
Solve MySQL (1064) error: 1064 - you have an error in your SQL syntax;
面部关键点检测-CNN
59 get prototype size