admin 管理员组

文章数量: 887021

http

背景

  • 项目中不可避免的使用第三方api接口,但是如果使用apache-httpclient总是不可避免需要设置连接池等,即使每次拷贝对于项目来说也显得冗余
  • http-api-invoker的方式可以使用注解的方式,定义api接口

<!--变更http包依赖--><dependency><groupId>com.bj58.dadiyang</groupId><artifactId>http-api-invoker</artifactId><version>1.0.2</version><exclusions><exclusion><artifactId>fastjson</artifactId><groupId>com.alibaba</groupId></exclusion></exclusions></dependency>
  • @HttpApiScan({"com.bj58.xx.httpinvoker"}) 对于springboot项目需要在application类引入

@RetryPolicy
@HttpApi("${xxx}")
public interface ConfigSvcApi {@HttpReq(timeout = 10000,value = "/theme/saveThemeConfig", method = "POST")HttpApiResult<Void> saveThemeConfig(ThemeParam param);@HttpReq(timeout = 10000,value = "/theme/getThemeConfig")HttpApiResult<String> getThemeConfig(@Param("salesBspId") String salesBspId, @Param("configType") String configType);}

原理分析

@Documented
@Target({ElementType.TYPE})
@Import({HttpApiConfigurer.class})
@Retention(RetentionPolicy.RUNTIME)
public @interface HttpApiScan {String[] value() default {""};String[] configPaths() default {""};
}

1.HttpApiScan通过Import注解引入了HttpApiConfigurer 类

@Component
public class HttpApiConfigurer implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {private static final Logger logger = LoggerFactory.getLogger(HttpApiConfigurer.class);private static final String CLASSPATH_PRE = "classpath:";private static final String FILE_PRE = "file:";private ApplicationContext ctx;public HttpApiConfigurer() {}

2.HttpApiConfigurer实现了 BeanDefinitionRegistryPostProcessor, ApplicationContextAware 接口 ,会在spring ioc初始化的过程中调用,将通过注解引入的api定义成一个GenericBeanDefinition交给spring-ioc管理

本文标签: http