如何把第三方服務注冊到spring項目容器中
前言
最近業(yè)務部門向我們反饋一個問題,我們部門原先提供的組件突然用不了了。后面排查是因為我們提供出去的組件類沒有注入到spring 容器中,之前沒問題是因為業(yè)務部門的根包名跟我們組件的根包名是一樣,后續(xù)他們根包名換了,導致我們的組件類沒法注入到spring中,當時的解決方案是形如下
@SpringBootApplication(scanBasePackages = {"業(yè)務根包","組件根包"})就是在業(yè)務的啟動類上加上掃描組件根包。
雖然這樣的方式可以解決,但是事后復盤了一下,業(yè)務方是否需要了解組件根包?是否還有更優(yōu)雅一點的方式?本文就來聊聊如何把第三方服務注冊到我們項目的spring容器中
注入方式
1、注入的組件個數(shù)比較少
自動裝配機制 + @Bean的形式
示例:
@Configuration
@Slf4j
@EnableConfigurationProperties(XxlJobProperty.class)
public?class?XxlJobAutoConfiguration?{
????@Bean
????@ConditionalOnMissingBean
????public?XxlJobSpringExecutor?xxlJobExecutor(XxlJobProperty property)?{
????????log.info(">>>>>>>>>>> xxl-job config init.");
????????XxlJobSpringExecutor xxlJobSpringExecutor =?new?XxlJobSpringExecutor();
????????xxlJobSpringExecutor.setAdminAddresses(property.getAdminAddresses());
????????xxlJobSpringExecutor.setAppname(property.getExecutorAppname());
????????xxlJobSpringExecutor.setAddress(property.getExecutorAddress());
????????xxlJobSpringExecutor.setIp(property.getExecutorIp());
????????xxlJobSpringExecutor.setPort(property.getExecutorPort());
????????xxlJobSpringExecutor.setAccessToken(property.getAccessToken());
????????xxlJobSpringExecutor.setLogPath(property.getExecutorLogPath());
????????xxlJobSpringExecutor.setLogRetentionDays(property.getExecutorLogRetentionDays());
????????return?xxlJobSpringExecutor;
????}在META-INF/spring.factories加入
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.github.lybgeek.autoconfiure.XxlJobAutoConfiguration
利用@Eanblexxx + @Import機制
示例:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloSeviceImpl.class)
public?@interface?EnableHelloSvc{
}在業(yè)務項目啟動加上@EnableHelloSvc
調(diào)用beanFactory.registerSingleton()
示例:
@Slf4j
public?class?HelloSvcBeanFactoryPostProcessor?implements?BeanFactoryPostProcessor?{
????@Override
????public?void?postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)?throws?BeansException?{
????????String beanName = StringUtils.uncapitalize(HelloService.class.getSimpleName());
????????log.info("register bean : beanName:{}",beanName);
????????beanFactory.registerSingleton(beanName,new?HelloServiceImpl());
????}
}2、注入的組件個數(shù)比較多
自動裝配機制 + @ComponentScan
示例:
@Configuration
@ComponentScan(basePackages = Constant.SVC_PACAKAEE)
public class ThirdPartySvcAutoConfiguration {
}在META-INF/spring.factories加入
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.github.lybgeek.autoconfiure.ThirdPartySvcAutoConfiguration
@Eanblexxx + @Import機制+ClassPathScanningCandidateComponentProvider
示例:
public?class?ThirdPartySvcRegister?implements?ImportBeanDefinitionRegistrar?{
????@Override
????public?void?registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry)?{
????????ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner =?new?ClassPathBeanDefinitionScanner(registry);
????????classPathBeanDefinitionScanner.scan(Constant.SVC_PACAKAEE);
????}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ThirdPartySvcRegister.class)
public?@interface?EnableThirdPartySvc {
}在業(yè)務項目啟動加上@EnableThirdPartySvc
總結(jié)
如果是業(yè)務開發(fā)人員直接使用
@SpringBootApplication(scanBasePackages = {"業(yè)務根包","組件根包"})其實是沒問題的,但是如果作為組件提供給其他業(yè)務部門使用,能讓業(yè)務部門無感知,開箱即用會是比較優(yōu)雅的方式
以上就是如何把第三方服務注冊到spring項目容器中的詳細內(nèi)容,更多關于spring注冊第三方服務注冊的資料請關注腳本之家其它相關文章!
相關文章
Java使用Fastjson2處理JSON字段大小寫不一致的優(yōu)雅方案
這篇文章主要為大家詳細介紹了Java使用Fastjson2處理JSON字段大小寫不一致的優(yōu)雅方案,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2026-04-04
關于接口ApplicationContext中的getBean()方法使用
這篇文章主要介紹了關于接口ApplicationContext中的getBean()方法使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
spring中IOC控制反轉(zhuǎn)依賴注入和new對象的區(qū)別說明
這篇文章主要介紹了spring中IOC控制反轉(zhuǎn)依賴注入和new對象的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
springboot + vue 實現(xiàn)遞歸生成多級菜單(實例代碼)
這篇文章主要介紹了springboot + vue 實現(xiàn)遞歸生成多級菜單,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
Java集合與數(shù)組區(qū)別簡介及相互轉(zhuǎn)換實例
這篇文章主要介紹了Java集合與數(shù)組區(qū)別簡介及相互轉(zhuǎn)換實例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Spring MVC訪問靜態(tài)文件_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Spring MVC訪問靜態(tài)文件的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

