Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a414a1

Browse files
committed
Merge branch 'master' into develop
2 parents 11615ad + 36d5ca7 commit 9a414a1

File tree

11 files changed

+85
-36
lines changed

11 files changed

+85
-36
lines changed

‎README.md‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.lvyahui8/spring-boot-data-aggregator-starter/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.lvyahui8/spring-boot-data-aggregator-starter)
77
[![GitHub release](https://img.shields.io/github/release/lvyahui8/spring-boot-data-aggregator.svg)](https://github.com/lvyahui8/spring-boot-data-aggregator/releases)
88

9+
[![Total alerts](https://img.shields.io/lgtm/alerts/g/lvyahui8/spring-boot-data-aggregator.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/lvyahui8/spring-boot-data-aggregator/alerts/)
10+
[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/lvyahui8/spring-boot-data-aggregator.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/lvyahui8/spring-boot-data-aggregator/context:java)
11+
912
## 背景与目的
1013

1114
在开发后台接口时, 为了开发效率, 我们往往习惯编写串行执行的代码, 去调用不同的接口, 即使这些接口之间并无依赖, 这使得最后开发的接口性能低下, 且数据不方便复用
@@ -107,17 +110,12 @@ public class UserServiceImpl implements UserService {
107110

108111
### 2. 调用聚合接口
109112

110-
```java
111-
@Autowired
112-
DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
113-
```
114-
115113
#### 方式一: 函数式调用
116114

117115
注意这里不能将函数式调用改为Lambda表达式, 两者的实际行为是不一致的.
118116

119117
```java
120-
User user = dataBeanAggregateQueryFacade.get(
118+
User user = DataFacade.get(
121119
Collections.singletonMap("userId", 1L),
122120
new Function2<User, List<Post>, User>() {
123121
@Override
@@ -151,7 +149,7 @@ public class UserAggregate {
151149
指定要查询的data id, 查询参数, 返回值类型, 并调用`facade.get`方法即可
152150

153151
```java
154-
User user = dataBeanAggregateQueryFacade.get(/*data id*/ "userWithPosts",
152+
User user = DataFacade.get(/*data id*/ "userWithPosts",
155153
/*Invoke Parameters*/
156154
Collections.singletonMap("userId",1L),
157155
User.class);

‎README_EN.md‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.lvyahui8/spring-boot-data-aggregator-starter/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.lvyahui8/spring-boot-data-aggregator-starter)
77
[![GitHub release](https://img.shields.io/github/release/lvyahui8/spring-boot-data-aggregator.svg)](https://github.com/lvyahui8/spring-boot-data-aggregator/releases)
88

9+
[![Total alerts](https://img.shields.io/lgtm/alerts/g/lvyahui8/spring-boot-data-aggregator.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/lvyahui8/spring-boot-data-aggregator/alerts/)
10+
[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/lvyahui8/spring-boot-data-aggregator.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/lvyahui8/spring-boot-data-aggregator/context:java)
11+
912
## Background and purpose
1013

1114
When developing the background interface, in order to improve the development efficiency, we often write serial execution codes to call different interfaces, even if there is no dependency among these interfaces, which causes the last developed interface performance is low, and the data is not convenient to reuse.
@@ -110,15 +113,11 @@ public class UserServiceImpl implements UserService {
110113

111114
### 2. Call the aggregation interface
112115

113-
```java
114-
@Autowired
115-
DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
116-
```
117-
118116
#### Method 1: Functional call
119117

118+
120119
```java
121-
User user = dataBeanAggregateQueryFacade.get(
120+
User user = DataFacade.get(
122121
Collections.singletonMap("userId", 1L),
123122
new Function2<User, List<Post>, User>() {
124123
@Override
@@ -152,8 +151,7 @@ public class UserAggregate {
152151
Specify queried data id, invoke parameters, and return type to invoke `facade.get` method
153152

154153
```java
155-
DataBeanAggregateQueryFacade queryFacade = context.getBean(DataBeanAggregateQueryFacade.class);
156-
User user = queryFacade.get(/*data id*/ "userWithPosts",
154+
User user = DataFacade.get(/*data id*/ "userWithPosts",
157155
/*Invoke Parameters*/
158156
Collections.singletonMap("userId",1L),
159157
User.class);

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
<groupId>io.github.lvyahui8</groupId>
2222
<artifactId>spring-boot-data-aggregator</artifactId>
23-
<version>1.1.3-SNAPSHOT</version>
23+
<version>1.1.3</version>
2424
<modules>
2525
<module>spring-boot-data-aggregator-core</module>
2626
<module>spring-boot-data-aggregator-autoconfigure</module>

‎spring-boot-data-aggregator-autoconfigure/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>spring-boot-data-aggregator</artifactId>
77
<groupId>io.github.lvyahui8</groupId>
8-
<version>1.1.3-SNAPSHOT</version>
8+
<version>1.1.3</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.github.lvyahui8.spring.aggregate.service.impl.DataBeanAggregateServiceImpl;
1515
import io.github.lvyahui8.spring.aggregate.util.DefinitionUtils;
1616
import io.github.lvyahui8.spring.annotation.DataProvider;
17+
import io.github.lvyahui8.spring.facade.FacadeInitializer;
1718
import lombok.extern.slf4j.Slf4j;
1819
import org.apache.commons.lang3.StringUtils;
1920
import org.reflections.Reflections;
@@ -64,7 +65,9 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
6465
@ConditionalOnMissingBean
6566
public DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade(
6667
@Qualifier("dataProviderRepository") DataProviderRepository dataProviderRepository) {
67-
return new DataBeanAggregateQueryFacadeImpl(dataBeanAggregateQueryService(dataProviderRepository));
68+
DataBeanAggregateQueryFacadeImpl facade = new DataBeanAggregateQueryFacadeImpl(dataBeanAggregateQueryService(dataProviderRepository));
69+
FacadeInitializer.initFacade(facade);
70+
return facade;
6871
}
6972

7073
private void checkCycle(Map<String,Set<String>> graphAdjMap) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.lvyahui8.spring.facade;
2+
3+
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
4+
import io.github.lvyahui8.spring.aggregate.func.MultipleArgumentsFunction;
5+
import lombok.AccessLevel;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.util.Map;
11+
12+
/**
13+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
14+
* @since 2020年2月13日
15+
*/
16+
public class DataFacade {
17+
18+
@Setter(AccessLevel.PACKAGE)
19+
@Getter(AccessLevel.PACKAGE)
20+
private static DataBeanAggregateQueryFacade facade;
21+
22+
public static <T> T get(String id, Map<String,Object> invokeParams, Class<T> clazz)
23+
throws InterruptedException, IllegalAccessException, InvocationTargetException {
24+
return facade.get(id,invokeParams,clazz);
25+
}
26+
27+
public static <T> T get(Map<String,Object> invokeParams, MultipleArgumentsFunction<T> multipleArgumentsFunction)
28+
throws InterruptedException, IllegalAccessException, InvocationTargetException {
29+
return facade.get(invokeParams,multipleArgumentsFunction);
30+
}
31+
32+
public static <T> T get(Map<String,Object> invokeParams, MultipleArgumentsFunction<T> multipleArgumentsFunction,Long timeout)
33+
throws InterruptedException, IllegalAccessException, InvocationTargetException {
34+
return facade.get(invokeParams,multipleArgumentsFunction,timeout);
35+
}
36+
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.lvyahui8.spring.facade;
2+
3+
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
4+
5+
/**
6+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
7+
* @since 2020年2月13日
8+
*/
9+
public class FacadeInitializer {
10+
public static void initFacade(DataBeanAggregateQueryFacade facade) {
11+
if (DataFacade.getFacade() != null) {
12+
throw new UnsupportedOperationException("DataFacade can be initialized only once.");
13+
}
14+
DataFacade.setFacade(facade);
15+
}
16+
}

‎spring-boot-data-aggregator-core/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>spring-boot-data-aggregator</artifactId>
77
<groupId>io.github.lvyahui8</groupId>
8-
<version>1.1.3-SNAPSHOT</version>
8+
<version>1.1.3</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

‎spring-boot-data-aggregator-example/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>spring-boot-data-aggregator</artifactId>
77
<groupId>io.github.lvyahui8</groupId>
8-
<version>1.1.3-SNAPSHOT</version>
8+
<version>1.1.3</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

‎spring-boot-data-aggregator-example/src/test/java/io/github/lvyahui8/spring/example/DataBeanAggregateQueryFacadeTest.java‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.collect.ImmutableMap;
44
import com.google.common.collect.Lists;
5-
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
65
import io.github.lvyahui8.spring.aggregate.func.Function2;
76
import io.github.lvyahui8.spring.aggregate.func.Function3;
87
import io.github.lvyahui8.spring.annotation.DataConsumer;
@@ -14,6 +13,7 @@
1413
import io.github.lvyahui8.spring.example.model.Category;
1514
import io.github.lvyahui8.spring.example.model.Post;
1615
import io.github.lvyahui8.spring.example.model.User;
16+
import io.github.lvyahui8.spring.facade.DataFacade;
1717
import lombok.extern.slf4j.Slf4j;
1818
import org.junit.Test;
1919
import org.junit.runner.RunWith;
@@ -37,9 +37,6 @@ public class DataBeanAggregateQueryFacadeTest {
3737

3838
private static final int NUM = 100;
3939

40-
@Autowired
41-
private DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
42-
4340
@Autowired
4441
private BeanAggregateProperties beanAggregateProperties;
4542

@@ -49,7 +46,7 @@ public class DataBeanAggregateQueryFacadeTest {
4946
@Test
5047
public void testSample() throws Exception {
5148
{
52-
User user = dataBeanAggregateQueryFacade.get("userWithPosts", Collections.singletonMap("userId",1L), User.class);
49+
User user = DataFacade.get("userWithPosts", Collections.singletonMap("userId",1L), User.class);
5350
Assert.notNull(user,"user not null");
5451
Assert.notNull(user.getPosts(),"user posts not null");
5552
log.info("user.name:{},user.posts.size:{}",
@@ -67,7 +64,7 @@ public void testSample() throws Exception {
6764

6865
{
6966
for (int i = 0; i < NUM; i ++) {
70-
String s = dataBeanAggregateQueryFacade.get("categoryTitle", Collections.singletonMap("categoryId", 1L), String.class);
67+
String s = DataFacade.get("categoryTitle", Collections.singletonMap("categoryId", 1L), String.class);
7168
Assert.isTrue(org.apache.commons.lang3.StringUtils.isNotEmpty(s),"s not null");
7269
}
7370
}
@@ -78,13 +75,13 @@ public void testExceptionProcessing() throws Exception {
7875
boolean success = false;
7976
if(! beanAggregateProperties.isIgnoreException()) {
8077
try {
81-
dataBeanAggregateQueryFacade.get("userWithPosts",
78+
DataFacade.get("userWithPosts",
8279
Collections.singletonMap("userId", 1L), User.class);
8380
} catch (Exception e) {
8481
log.info("default settings is SUSPEND, catch an exception: {}",e.getMessage(),e);
8582
}
8683
} else {
87-
User user = dataBeanAggregateQueryFacade.get("userWithPosts",
84+
User user = DataFacade.get("userWithPosts",
8885
Collections.singletonMap("userId", 1L), User.class);
8986
Assert.notNull(user,"user must be not null!");
9087
}
@@ -93,7 +90,7 @@ public void testExceptionProcessing() throws Exception {
9390
@Test
9491
public void testGetByMultipleArgumentsFunction() throws Exception {
9592
Map<String, Object> singletonMap = Collections.singletonMap("userId", 1L);
96-
User user = dataBeanAggregateQueryFacade.get(singletonMap, new Function2<User, List<Post>, User>() {
93+
User user = DataFacade.get(singletonMap, new Function2<User, List<Post>, User>() {
9794
@Override
9895
public User apply(@DataConsumer("user") User user, @DataConsumer("posts") List<Post> posts) {
9996
user.setPosts(posts);
@@ -102,7 +99,7 @@ public User apply(@DataConsumer("user") User user, @DataConsumer("posts") List<P
10299
},null);
103100
Assert.notNull(user,"user never not be null!");
104101
try {
105-
user = dataBeanAggregateQueryFacade.get(singletonMap, (Function2<User, List<Post>, User>) (user1, posts) -> {
102+
user = DataFacade.get(singletonMap, (Function2<User, List<Post>, User>) (user1, posts) -> {
106103
user1.setPosts(posts);
107104
return user1;
108105
},null);
@@ -118,7 +115,7 @@ public void testInheritableThreadLocals() throws Exception {
118115
user.setUsername("bob");
119116
user.setId(100000L);
120117
ExampleAppContext.setLoggedUser(user);
121-
dataBeanAggregateQueryFacade.get(null, new Function2<String,List<User>,User>() {
118+
DataFacade.get(null, new Function2<String,List<User>,User>() {
122119
@Override
123120
public User apply(@DataConsumer("loggedUsername") String loggedUsername,
124121
@DataConsumer("loggedUserFollowers") List<User> loggedUserFollowers) {
@@ -138,7 +135,7 @@ public User apply(@DataConsumer("loggedUsername") String loggedUsername,
138135
public void testThreadLocal() throws Exception {
139136
try {
140137
RequestContext.setTenantId(10000L);
141-
Object result = dataBeanAggregateQueryFacade.get(null,
138+
Object result = DataFacade.get(null,
142139
new Function3<List<Category>, List<Post>, List<User>, Object>() {
143140
@Override
144141
public Object apply(
@@ -161,7 +158,7 @@ public void testDynamicParameter() throws Exception {
161158
.put("userA_Id", 1L)
162159
.put("userB_Id", 2L)
163160
.put("userC_Id", 3L).build();
164-
Object specialUserCollection = dataBeanAggregateQueryFacade.get(params,
161+
Object specialUserCollection = DataFacade.get(params,
165162
new Function3<User,User,User,Object>() {
166163
@Override
167164
public Object apply(
@@ -181,7 +178,7 @@ public Object apply(
181178
@Test
182179
public void testParameterTypeException() throws Exception {
183180
try{
184-
dataBeanAggregateQueryFacade.get(Collections.singletonMap("userId", "1"),
181+
DataFacade.get(Collections.singletonMap("userId", "1"),
185182
new Function2<User, List<Post>, User>() {
186183
@Override
187184
public User apply(@DataConsumer("user") User user,
@@ -209,7 +206,7 @@ public String apply(@DataConsumer("rootCategory") Category category,
209206
Exception exp = null;
210207
for (int i = 0; i < 1000; i ++) {
211208
try {
212-
String name = dataBeanAggregateQueryFacade.get(map,userFunction);
209+
String name = DataFacade.get(map,userFunction);
213210
} catch (Exception e) {
214211
exp = e;
215212
log.error("exp:" + e.getMessage());

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /