Skip to content

Commit 39fc994

Browse files
optimize: ServiceFactory未添加注解时,提示正确的异常信息。
1 parent fb5c2c8 commit 39fc994

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

core-parent/core/src/main/java/icu/easyj/core/factory/ServiceFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void addService(S service) {
142142
}
143143

144144
public void addServiceInfo(ServiceInfo<S> serviceInfo) {
145-
this.serviceMap.computeIfAbsent(serviceInfo.getCode(), ServiceGroup::new).add(serviceInfo);
145+
this.serviceMap.computeIfAbsent(serviceInfo.getCode().toLowerCase(), ServiceGroup::new).add(serviceInfo);
146146

147147
if (serviceInfo.isDefault() && !DEFAULT_SERVICE_CODE.equals(serviceInfo.getCode())) {
148148
this.serviceMap.computeIfAbsent(DEFAULT_SERVICE_CODE, ServiceGroup::new).add(serviceInfo);
@@ -157,15 +157,15 @@ public void addServiceList(List<S> serviceList) {
157157
HashMap<String, List<ServiceInfo<S>>> map = new HashMap<>();
158158
for (S service : serviceList) {
159159
ServiceInfo<S> serviceInfo = ServiceInfo.of(service);
160-
map.computeIfAbsent(serviceInfo.getCode(), k -> new ArrayList<>()).add(serviceInfo);
160+
map.computeIfAbsent(serviceInfo.getCode().toLowerCase(), k -> new ArrayList<>()).add(serviceInfo);
161161

162162
if (serviceInfo.isDefault() && !DEFAULT_SERVICE_CODE.equals(serviceInfo.getCode())) {
163163
map.computeIfAbsent(DEFAULT_SERVICE_CODE, k -> new ArrayList<>()).add(serviceInfo);
164164
}
165165
}
166166

167167
map.forEach((code, serviceInfoList) -> {
168-
ServiceGroup<S> group = this.serviceMap.computeIfAbsent(code, ServiceGroup::new);
168+
ServiceGroup<S> group = this.serviceMap.computeIfAbsent(code.toLowerCase(), ServiceGroup::new);
169169
group.addAll(serviceInfoList);
170170
});
171171
}
@@ -177,15 +177,15 @@ public void addServiceInfoList(List<ServiceInfo<S>> serviceInfoList) {
177177

178178
HashMap<String, List<ServiceInfo<S>>> map = new HashMap<>();
179179
for (ServiceInfo<S> serviceInfo : serviceInfoList) {
180-
map.computeIfAbsent(serviceInfo.getCode(), k -> new ArrayList<>()).add(serviceInfo);
180+
map.computeIfAbsent(serviceInfo.getCode().toLowerCase(), k -> new ArrayList<>()).add(serviceInfo);
181181

182182
if (serviceInfo.isDefault() && !DEFAULT_SERVICE_CODE.equals(serviceInfo.getCode())) {
183183
map.computeIfAbsent(DEFAULT_SERVICE_CODE, k -> new ArrayList<>()).add(serviceInfo);
184184
}
185185
}
186186

187187
map.forEach((code, serviceInfoList0) -> {
188-
ServiceGroup<S> group = this.serviceMap.computeIfAbsent(code, ServiceGroup::new);
188+
ServiceGroup<S> group = this.serviceMap.computeIfAbsent(code.toLowerCase(), ServiceGroup::new);
189189
group.addAll(serviceInfoList0);
190190
});
191191
}

core-parent/core/src/main/java/icu/easyj/core/factory/ServiceGroup.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20-
import java.util.Objects;
2120

2221
import javax.annotation.Nonnull;
2322
import javax.annotation.Nullable;
@@ -52,7 +51,7 @@ public ServiceGroup(String code) {
5251
throw new IllegalArgumentException("code 不能为空");
5352
}
5453

55-
this.code = code.toLowerCase();
54+
this.code = code;
5655
this.sortedServiceList = new ArrayList<>();
5756
}
5857

@@ -133,7 +132,7 @@ private void checkServiceInfo(ServiceInfo<S> serviceInfo) {
133132
return;
134133
}
135134

136-
if (!Objects.equals(this.code, serviceInfo.getCode())) {
135+
if (!this.code.equalsIgnoreCase(serviceInfo.getCode())) {
137136
throw new IllegalArgumentException("serviceInfo 的 code 与 ServiceGroup 的 code 不一致");
138137
}
139138
}

core-parent/core/src/main/java/icu/easyj/core/factory/ServiceInfo.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import javax.annotation.Nonnull;
1919

2020
import icu.easyj.core.util.StringUtils;
21+
import org.springframework.aop.support.AopUtils;
2122
import org.springframework.core.Ordered;
23+
import org.springframework.core.annotation.AnnotationConfigurationException;
2224

2325
/**
2426
* 服务信息
@@ -36,7 +38,7 @@ public class ServiceInfo<S> {
3638
private final int order;
3739

3840
private ServiceInfo(String code, boolean isDefault, int order, S service) {
39-
this.code = code.toLowerCase(); // 转为小写
41+
this.code = code; // 转为小写
4042
this.isDefault = isDefault;
4143
this.order = order;
4244
this.service = service;
@@ -48,7 +50,13 @@ public static <S> ServiceInfo<S> of(S service) {
4850
throw new IllegalArgumentException("service不能为空");
4951
}
5052

51-
ServiceMark mark = service.getClass().getAnnotation(ServiceMark.class);
53+
Class<?> serviceClass = AopUtils.getTargetClass(service);
54+
55+
ServiceMark mark = serviceClass.getAnnotation(ServiceMark.class);
56+
if (mark == null) {
57+
throw new AnnotationConfigurationException(serviceClass.getName() + " 未添加注解 @ServiceMark(code = \"xxx\")");
58+
}
59+
5260
String code = mark.code();
5361
if (StringUtils.isBlank(code)) {
5462
throw new IllegalArgumentException("service的@ServiceMark.code或@ServiceMark.value不能为空");

core-parent/core/src/test/java/icu/easyj/core/factory/ServiceFactoryTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import java.util.List;
55

66
import org.junit.jupiter.api.Test;
7+
import org.springframework.core.annotation.AnnotationConfigurationException;
78

8-
import static org.junit.jupiter.api.Assertions.*;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertNull;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
912

1013
class ServiceFactoryTest {
1114

@@ -39,6 +42,9 @@ void test() {
3942
assertEquals(b1, bList.get(0));
4043
// B2
4144
assertEquals(b2, bList.get(1));
45+
// TestServiceImplNoAnnotation
46+
assertNull(serviceFactory.get(TestServiceImplNoAnnotation.class.getSimpleName())); // null
47+
assertThrows(AnnotationConfigurationException.class, () -> serviceFactory.addService(new TestServiceImplNoAnnotation())); // throw AnnotationConfigurationException
4248
}
4349

4450
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package icu.easyj.core.factory;
2+
3+
class TestServiceImplNoAnnotation implements ITestService {
4+
}

0 commit comments

Comments
 (0)