diff --git a/README.md b/README.md index fe69ad7..de2e236 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 3.0.0 + 3.0.1 ``` @@ -177,6 +177,8 @@ retrofit: enable: false # 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置 circuit-breaker-config-name: defaultCircuitBreakerConfig + # 自动设置PathMathInterceptor的scope为prototype + auto-set-prototype-scope-for-path-math-interceptor: true ``` ## 高级功能 @@ -260,7 +262,21 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor { return chain.proceed(newRequest); } } +``` + +默认情况下,**组件会自动将`BasePathMatchInterceptor`的`scope`设置为`prototype`**。 +可通过`retrofit.auto-set-prototype-scope-for-path-math-interceptor=false`关闭该功能。关闭之后,需要手动将`scope`设置为`prototype`。 + +```java +@Component +@Scope("prototype") +public class TimeStampInterceptor extends BasePathMatchInterceptor { + @Override + public Response doIntercept(Chain chain) throws IOException { + // ... + } +} ``` #### 接口上使用`@Intercept`进行标注 @@ -281,6 +297,8 @@ public interface HttpApi { 上面的`@Intercept`配置表示:拦截`HttpApi`接口下`/api/**`路径下(排除`/api/test/savePerson`)的请求,拦截处理器使用`TimeStampInterceptor`。 + + ### 自定义拦截注解 有的时候,我们需要在"拦截注解"动态传入一些参数,然后在拦截的时候使用这些参数。 这时候,我们可以使用"自定义拦截注解",步骤如下: diff --git a/README_EN.md b/README_EN.md index b6476ff..c7b802d 100644 --- a/README_EN.md +++ b/README_EN.md @@ -41,7 +41,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 3.0.0 + 3.0.1 ``` @@ -141,6 +141,7 @@ retrofit: enable: false # Get CircuitBreakerConfig from {@link CircuitBreakerConfigRegistry} based on this name as a global circuit breaker configuration circuit-breaker-config-name: defaultCircuitBreakerConfig + auto-set-prototype-scope-for-path-math-interceptor: true ``` ## Advanced Features @@ -234,7 +235,21 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor { return chain.proceed(newRequest); } } +``` + +By default, **component will automatically set `scope` of `BasePathMatchInterceptor` to `prototype`**. +This feature can be turned off by `retrofit.auto-set-prototype-scope-for-path-math-interceptor=false`. After closing, you need to manually set `scope` to `prototype`. +```java +@Component +@Scope("prototype") +public class TimeStampInterceptor extends BasePathMatchInterceptor { + + @Override + public Response doIntercept(Chain chain) throws IOException { + // ... + } +} ``` #### Use the `@Intercept` annotation to specify the interceptor to use diff --git a/pom.xml b/pom.xml index 38ef8c9..7795b3c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 3.0.0 + 3.0.1 retrofit-spring-boot-starter retrofit-spring-boot-starter diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java index d91fa8d..820a0d2 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java @@ -56,7 +56,8 @@ public RetrofitAutoConfiguration(RetrofitProperties retrofitProperties) { public static class RetrofitAdvanceConfiguration { @Bean - @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = "retrofit", name = "auto-set-prototype-scope-for-path-math-interceptor", + matchIfMissing = true) public static PathMatchInterceptorBdfProcessor pathMatchInterceptorBdfProcessor() { return new PathMatchInterceptorBdfProcessor(); } @@ -106,7 +107,8 @@ public ServiceInstanceChooser retrofitServiceInstanceChooser() { @Bean @ConditionalOnMissingBean - public ServiceChooseInterceptor retrofitServiceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) { + public ServiceChooseInterceptor + retrofitServiceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) { return new ServiceChooseInterceptor(serviceInstanceChooser); } @@ -168,7 +170,8 @@ public CircuitBreakerConfigRegistry retrofitCircuitBreakerConfigRegistry( @Bean @ConditionalOnMissingBean - public RetrofitDegrade retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigRegistry circuitBreakerConfigRegistry) { + public RetrofitDegrade + retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigRegistry circuitBreakerConfigRegistry) { return new Resilience4jRetrofitDegrade(CircuitBreakerRegistry.ofDefaults(), properties.getDegrade().getGlobalResilience4jDegrade(), circuitBreakerConfigRegistry); } @@ -179,17 +182,17 @@ public RetrofitDegrade retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigR @EnableConfigurationProperties(RetrofitProperties.class) public static class SentinelConfiguration { - private final RetrofitProperties properties; + private final RetrofitProperties properties; - public SentinelConfiguration(RetrofitProperties properties) { - this.properties = properties; - } + public SentinelConfiguration(RetrofitProperties properties) { + this.properties = properties; + } - @Bean - @ConditionalOnMissingBean - public RetrofitDegrade retrofitSentinelRetrofitDegrade() { - return new SentinelRetrofitDegrade(properties.getDegrade().getGlobalSentinelDegrade()); - } + @Bean + @ConditionalOnMissingBean + public RetrofitDegrade retrofitSentinelRetrofitDegrade() { + return new SentinelRetrofitDegrade(properties.getDegrade().getGlobalSentinelDegrade()); } + } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java index 8fe28d3..f444725 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java @@ -21,6 +21,11 @@ @Data public class RetrofitProperties { + /** + * 自动设置PathMathInterceptor的scope为prototype + */ + private boolean autoSetPrototypeScopeForPathMathInterceptor = true; + /** * 全局重试配置 *

diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index eec81b0..6bba9eb 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -63,6 +63,8 @@ retrofit: enable: false # 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置 circuit-breaker-config-name: defaultCircuitBreakerConfig + # 自动设置PathMathInterceptor的scope为prototype + auto-set-prototype-scope-for-path-math-interceptor: true test: baseUrl: http://localhost:8080/api/test/ diff --git a/src/test/resources/default-config.yml b/src/test/resources/default-config.yml index dfc5193..b23adc9 100644 --- a/src/test/resources/default-config.yml +++ b/src/test/resources/default-config.yml @@ -60,3 +60,5 @@ retrofit: enable: false # 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置 circuit-breaker-config-name: defaultCircuitBreakerConfig + # 自动设置PathMathInterceptor的scope为prototype + auto-set-prototype-scope-for-path-math-interceptor: true \ No newline at end of file