Skip to content

Commit 30894f3

Browse files
autoSetPrototypeScopeForPathMathInterceptor config
1 parent 74f6a90 commit 30894f3

File tree

7 files changed

+60
-15
lines changed

7 files changed

+60
-15
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
5151
<dependency>
5252
<groupId>com.github.lianjiatech</groupId>
5353
<artifactId>retrofit-spring-boot-starter</artifactId>
54-
<version>3.0.0</version>
54+
<version>3.0.1</version>
5555
</dependency>
5656
```
5757

@@ -177,6 +177,8 @@ retrofit:
177177
enable: false
178178
# 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置
179179
circuit-breaker-config-name: defaultCircuitBreakerConfig
180+
# 自动设置PathMathInterceptor的scope为prototype
181+
auto-set-prototype-scope-for-path-math-interceptor: true
180182
```
181183
182184
## 高级功能
@@ -260,7 +262,21 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor {
260262
return chain.proceed(newRequest);
261263
}
262264
}
265+
```
266+
267+
默认情况下,**组件会自动将`BasePathMatchInterceptor`的`scope`设置为`prototype`**。
268+
可通过`retrofit.auto-set-prototype-scope-for-path-math-interceptor=false`关闭该功能。关闭之后,需要手动将`scope`设置为`prototype`。
269+
270+
```java
271+
@Component
272+
@Scope("prototype")
273+
public class TimeStampInterceptor extends BasePathMatchInterceptor {
263274
275+
@Override
276+
public Response doIntercept(Chain chain) throws IOException {
277+
// ...
278+
}
279+
}
264280
```
265281

266282
#### 接口上使用`@Intercept`进行标注
@@ -281,6 +297,8 @@ public interface HttpApi {
281297

282298
上面的`@Intercept`配置表示:拦截`HttpApi`接口下`/api/**`路径下(排除`/api/test/savePerson`)的请求,拦截处理器使用`TimeStampInterceptor`。
283299

300+
301+
284302
### 自定义拦截注解
285303

286304
有的时候,我们需要在"拦截注解"动态传入一些参数,然后在拦截的时候使用这些参数。 这时候,我们可以使用"自定义拦截注解",步骤如下:

README_EN.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>com.github.lianjiatech</groupId>
4343
<artifactId>retrofit-spring-boot-starter</artifactId>
44-
<version>3.0.0</version>
44+
<version>3.0.1</version>
4545
</dependency>
4646
```
4747

@@ -141,6 +141,7 @@ retrofit:
141141
enable: false
142142
# Get CircuitBreakerConfig from {@link CircuitBreakerConfigRegistry} based on this name as a global circuit breaker configuration
143143
circuit-breaker-config-name: defaultCircuitBreakerConfig
144+
auto-set-prototype-scope-for-path-math-interceptor: true
144145
```
145146
146147
## Advanced Features
@@ -234,7 +235,21 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor {
234235
return chain.proceed(newRequest);
235236
}
236237
}
238+
```
239+
240+
By default, **component will automatically set `scope` of `BasePathMatchInterceptor` to `prototype`**.
241+
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`.
237242

243+
```java
244+
@Component
245+
@Scope("prototype")
246+
public class TimeStampInterceptor extends BasePathMatchInterceptor {
247+
248+
@Override
249+
public Response doIntercept(Chain chain) throws IOException {
250+
// ...
251+
}
252+
}
238253
```
239254

240255
#### Use the `@Intercept` annotation to specify the interceptor to use

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.lianjiatech</groupId>
88
<artifactId>retrofit-spring-boot-starter</artifactId>
9-
<version>3.0.0</version>
9+
<version>3.0.1</version>
1010

1111
<name>retrofit-spring-boot-starter</name>
1212
<description>retrofit-spring-boot-starter</description>

src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public RetrofitAutoConfiguration(RetrofitProperties retrofitProperties) {
5656
public static class RetrofitAdvanceConfiguration {
5757

5858
@Bean
59-
@ConditionalOnMissingBean
59+
@ConditionalOnProperty(prefix = "retrofit", name = "auto-set-prototype-scope-for-path-math-interceptor",
60+
matchIfMissing = true)
6061
public static PathMatchInterceptorBdfProcessor pathMatchInterceptorBdfProcessor() {
6162
return new PathMatchInterceptorBdfProcessor();
6263
}
@@ -106,7 +107,8 @@ public ServiceInstanceChooser retrofitServiceInstanceChooser() {
106107

107108
@Bean
108109
@ConditionalOnMissingBean
109-
public ServiceChooseInterceptor retrofitServiceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) {
110+
public ServiceChooseInterceptor
111+
retrofitServiceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) {
110112
return new ServiceChooseInterceptor(serviceInstanceChooser);
111113
}
112114

@@ -168,7 +170,8 @@ public CircuitBreakerConfigRegistry retrofitCircuitBreakerConfigRegistry(
168170

169171
@Bean
170172
@ConditionalOnMissingBean
171-
public RetrofitDegrade retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigRegistry circuitBreakerConfigRegistry) {
173+
public RetrofitDegrade
174+
retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigRegistry circuitBreakerConfigRegistry) {
172175
return new Resilience4jRetrofitDegrade(CircuitBreakerRegistry.ofDefaults(),
173176
properties.getDegrade().getGlobalResilience4jDegrade(), circuitBreakerConfigRegistry);
174177
}
@@ -179,17 +182,17 @@ public RetrofitDegrade retrofitResilience4jRetrofitDegrade(CircuitBreakerConfigR
179182
@EnableConfigurationProperties(RetrofitProperties.class)
180183
public static class SentinelConfiguration {
181184

182-
private final RetrofitProperties properties;
185+
private final RetrofitProperties properties;
183186

184-
public SentinelConfiguration(RetrofitProperties properties) {
185-
this.properties = properties;
186-
}
187+
public SentinelConfiguration(RetrofitProperties properties) {
188+
this.properties = properties;
189+
}
187190

188-
@Bean
189-
@ConditionalOnMissingBean
190-
public RetrofitDegrade retrofitSentinelRetrofitDegrade() {
191-
return new SentinelRetrofitDegrade(properties.getDegrade().getGlobalSentinelDegrade());
192-
}
191+
@Bean
192+
@ConditionalOnMissingBean
193+
public RetrofitDegrade retrofitSentinelRetrofitDegrade() {
194+
return new SentinelRetrofitDegrade(properties.getDegrade().getGlobalSentinelDegrade());
193195
}
196+
}
194197

195198
}

src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
@Data
2222
public class RetrofitProperties {
2323

24+
/**
25+
* 自动设置PathMathInterceptor的scope为prototype
26+
*/
27+
private boolean autoSetPrototypeScopeForPathMathInterceptor = true;
28+
2429
/**
2530
* 全局重试配置
2631
* <p>

src/test/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ retrofit:
6363
enable: false
6464
# 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置
6565
circuit-breaker-config-name: defaultCircuitBreakerConfig
66+
# 自动设置PathMathInterceptor的scope为prototype
67+
auto-set-prototype-scope-for-path-math-interceptor: true
6668

6769
test:
6870
baseUrl: http://localhost:8080/api/test/

src/test/resources/default-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ retrofit:
6060
enable: false
6161
# 根据该名称从#{@link CircuitBreakerConfigRegistry}获取CircuitBreakerConfig,作为全局熔断配置
6262
circuit-breaker-config-name: defaultCircuitBreakerConfig
63+
# 自动设置PathMathInterceptor的scope为prototype
64+
auto-set-prototype-scope-for-path-math-interceptor: true

0 commit comments

Comments
 (0)