共计 3049 个字符,预计需要花费 8 分钟才能阅读完成。
前言
最近业务部门向咱们反馈一个问题,咱们部门原先提供的组件忽然用不了了。前面排查是因为咱们提供进来的组件类没有注入到 spring 容器中,之前没问题是因为业务部门的根包名跟咱们组件的根包名是一样,后续他们根包名换了,导致咱们的组件类没法注入到 spring 中,过后的解决方案是形如下
@SpringBootApplication(scanBasePackages = {"业务根包","组件根包"})
就是在业务的启动类上加上扫描组件根包。
尽管这样的形式能够解决,然而预先复盘了一下,业务方是否须要理解组件根包?是否还有更优雅一点的形式?本文就来聊聊如何把第三方服务注册到咱们我的项目的 spring 容器中
注入形式
1、注入的组件个数比拟少
1、主动拆卸机制 + @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
2、利用 @Eanblexxx + @Import 机制
示例:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloSeviceImpl.class)
public @interface EnableHelloSvc{}
在业务我的项目启动加上 @EnableHelloSvc
3、调用 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、注入的组件个数比拟多
1、主动拆卸机制 + @ComponentScan
示例:
@Configuration
@ComponentScan(basePackages = Constant.SVC_PACAKAEE)
public class ThirdPartySvcAutoConfiguration {}
在 META-INF/spring.factories 退出
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.lybgeek.autoconfiure.ThirdPartySvcAutoConfiguration
2、@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 {}
在业务我的项目启动加上 @EnableThirdPartySvc
总结
如果是业务开发人员间接应用
@SpringBootApplication(scanBasePackages = {"业务根包","组件根包"})
其实是没问题的,然而如果作为组件提供给其余业务部门应用,能让业务部门无感知,开箱即用会是比拟优雅的形式
demo 链接
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-scan-thirdparty-service