Skip to content

Commit 5bb6bf0

Browse files
committed
修改拦截器定义方式, 并支持多个拦截器注解排序
1 parent 019fa61 commit 5bb6bf0

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/autoconfigure/BeanAggregateAutoConfiguration.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.lvyahui8.spring.aggregate.config.RuntimeSettings;
44
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
55
import io.github.lvyahui8.spring.aggregate.facade.impl.DataBeanAggregateQueryFacadeImpl;
6+
import io.github.lvyahui8.spring.aggregate.interceptor.AggregateQueryInterceptor;
67
import io.github.lvyahui8.spring.aggregate.interceptor.AggregateQueryInterceptorChain;
78
import io.github.lvyahui8.spring.aggregate.interceptor.impl.AggregateQueryInterceptorChainImpl;
89
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
@@ -24,14 +25,16 @@
2425
import org.springframework.context.ApplicationContextAware;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.Ordered;
2729
import org.springframework.core.annotation.AnnotationUtils;
30+
import org.springframework.core.annotation.Order;
2831
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
2932
import org.springframework.util.Assert;
3033
import org.springframework.util.StringUtils;
3134

3235
import java.lang.reflect.Method;
3336
import java.lang.reflect.Modifier;
34-
import java.util.Set;
37+
import java.util.*;
3538
import java.util.concurrent.ExecutorService;
3639
import java.util.concurrent.LinkedBlockingDeque;
3740
import java.util.concurrent.ThreadPoolExecutor;
@@ -104,7 +107,7 @@ public DataBeanAggregateQueryService dataBeanAggregateQueryService (
104107
* @return
105108
*/
106109
@Bean(name = "aggregateExecutorService")
107-
@ConditionalOnMissingBean(name = "aggregateExecutorService")
110+
@ConditionalOnMissingBean(name = "aggregateExecutorService",value=ExecutorService.class)
108111
public ExecutorService aggregateExecutorService() {
109112
return new ThreadPoolExecutor(
110113
properties.getThreadNumber(),
@@ -129,6 +132,24 @@ public DataProviderRepository dataProviderRepository() {
129132
@Bean(name = "aggregateQueryInterceptorChain")
130133
@ConditionalOnMissingBean(AggregateQueryInterceptorChain.class)
131134
public AggregateQueryInterceptorChain aggregateQueryInterceptorChain() {
132-
return new AggregateQueryInterceptorChainImpl();
135+
Map<String, AggregateQueryInterceptor> interceptorMap = applicationContext.getBeansOfType(AggregateQueryInterceptor.class);
136+
AggregateQueryInterceptorChainImpl interceptorChain = new AggregateQueryInterceptorChainImpl();
137+
if(interceptorMap != null && ! interceptorMap.isEmpty()) {
138+
List<AggregateQueryInterceptor> interceptors = new ArrayList<>(interceptorMap.values());
139+
interceptors.sort(new Comparator<AggregateQueryInterceptor>() {
140+
@Override
141+
public int compare(AggregateQueryInterceptor o1, AggregateQueryInterceptor o2) {
142+
Order order1 = o1.getClass().getAnnotation(Order.class);
143+
Order order2 = o2.getClass().getAnnotation(Order.class);
144+
int oi1 = order1 == null ? Ordered.LOWEST_PRECEDENCE : order1.value();
145+
int oi2 = order2 == null ? Ordered.LOWEST_PRECEDENCE : order2.value();
146+
return oi1 - oi2;
147+
}
148+
});
149+
for (AggregateQueryInterceptor interceptor : interceptors) {
150+
interceptorChain.addInterceptor(interceptor);
151+
}
152+
}
153+
return interceptorChain;
133154
}
134155
}

spring-boot-data-aggregator-example/src/main/java/io/github/lvyahui8/spring/example/configuration/AggregatorCustomConfiguration.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package io.github.lvyahui8.spring.example.configuration;
22

3-
import io.github.lvyahui8.spring.aggregate.interceptor.AggregateQueryInterceptorChain;
4-
import io.github.lvyahui8.spring.aggregate.interceptor.impl.AggregateQueryInterceptorChainImpl;
5-
import io.github.lvyahui8.spring.example.interceptor.SampleAggregateQueryInterceptor;
6-
import org.springframework.beans.factory.annotation.Qualifier;
73
import org.springframework.context.annotation.Bean;
84
import org.springframework.context.annotation.Configuration;
95
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
@@ -30,18 +26,4 @@ public ExecutorService aggregateExecutorService() {
3026
new LinkedBlockingDeque<>(1024),
3127
new CustomizableThreadFactory("example-async"));
3228
}
33-
34-
/**
35-
* 自定义拦截器处理链
36-
*
37-
* @param sampleAggregateQueryInterceptor
38-
* @return
39-
*/
40-
@Bean(name = "aggregateQueryInterceptorChain")
41-
public AggregateQueryInterceptorChain aggregateQueryInterceptorChain(
42-
@Qualifier("sampleAggregateQueryInterceptor") SampleAggregateQueryInterceptor sampleAggregateQueryInterceptor) {
43-
AggregateQueryInterceptorChainImpl aggregateQueryInterceptorChain = new AggregateQueryInterceptorChainImpl();
44-
aggregateQueryInterceptorChain.addInterceptor(sampleAggregateQueryInterceptor);
45-
return aggregateQueryInterceptorChain;
46-
}
4729
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.lvyahui8.spring.example.interceptor;
2+
3+
import io.github.lvyahui8.spring.aggregate.context.AggregationContext;
4+
import io.github.lvyahui8.spring.aggregate.interceptor.impl.AggregateQueryInterceptorAdapter;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.core.annotation.Order;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
11+
* @since 2019/9/7 23:02
12+
*/
13+
@Component
14+
@Order(1)
15+
@Slf4j
16+
public class PerSetupAggregateQueryInterceptor extends AggregateQueryInterceptorAdapter {
17+
@Override
18+
public boolean querySubmitted(AggregationContext aggregationContext) {
19+
log.info("current thread {}", Thread.currentThread().getName());
20+
return super.querySubmitted(aggregationContext);
21+
}
22+
}

spring-boot-data-aggregator-example/src/main/java/io/github/lvyahui8/spring/example/interceptor/SampleAggregateQueryInterceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import io.github.lvyahui8.spring.aggregate.interceptor.AggregateQueryInterceptor;
55
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.core.annotation.Order;
78
import org.springframework.stereotype.Component;
89

910
/**
1011
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
1112
* @since 2019/8/4 17:24
1213
*/
1314
@Component
15+
@Order(2)
1416
@Slf4j
1517
public class SampleAggregateQueryInterceptor implements AggregateQueryInterceptor {
1618
@Override

0 commit comments

Comments
 (0)