Skip to content

Commit 27c0283

Browse files
committed
优化entityFactory
1 parent 43d9f9c commit 27c0283

File tree

2 files changed

+149
-3
lines changed

2 files changed

+149
-3
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2016 http://www.hswebframework.org
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
*/
18+
19+
package org.hswebframework.web.starter;
20+
21+
import org.hswebframework.web.commons.entity.Entity;
22+
import org.hswebframework.web.commons.entity.factory.MapperEntityFactory;
23+
import org.hswebframwork.utils.MapUtils;
24+
import org.hswebframwork.utils.StringUtils;
25+
import org.springframework.boot.context.properties.ConfigurationProperties;
26+
import org.springframework.util.ClassUtils;
27+
28+
import java.lang.instrument.IllegalClassFormatException;
29+
import java.util.*;
30+
import java.util.stream.Collectors;
31+
32+
/**
33+
* TODO 完成注释
34+
* <p>
35+
* <pre>
36+
* hsweb:
37+
* entity:
38+
* mapping:
39+
* org.hswebframework.web.entity.user.UserEntity:com.company.entity.user.CustomUserEntity
40+
* </pre>
41+
*
42+
* @author zhouhao
43+
*/
44+
@ConfigurationProperties(prefix = "hsweb.entity")
45+
public class EntityProperties {
46+
List<Mapping> mappings;
47+
48+
public void setMappings(List<Mapping> mappings) {
49+
this.mappings = mappings;
50+
}
51+
52+
public List<Mapping> getMappings() {
53+
return mappings;
54+
}
55+
56+
public Map<Class<Entity>, MapperEntityFactory.Mapper> createMappers() {
57+
if (mappings == null || mappings.isEmpty()) return Collections.emptyMap();
58+
return mappings.stream()
59+
.map(Mapping::create)
60+
.reduce(MapUtils::merge)
61+
.get();
62+
}
63+
64+
public static class Mapping {
65+
String sourceBasePackage = "";
66+
String targetBasePackage = "";
67+
Map<String, String> mapping;
68+
69+
Map<Class<Entity>, MapperEntityFactory.Mapper> create() {
70+
if (mapping == null || mapping.isEmpty()) return Collections.emptyMap();
71+
return mapping.entrySet().stream()
72+
.collect(Collectors.toMap(
73+
entry -> getSourceClass(entry.getKey()),
74+
entry -> MapperEntityFactory.defaultMapper(getTargetClass(entry.getValue()))));
75+
}
76+
77+
protected Class<Entity> getClass(String basePackage, String name) {
78+
if (!StringUtils.isNullOrEmpty(basePackage))
79+
name = basePackage.concat(".").concat(name);
80+
return classForName(name);
81+
}
82+
83+
protected Class<Entity> getSourceClass(String name) {
84+
return getClass(sourceBasePackage, name);
85+
}
86+
87+
protected Class<Entity> getTargetClass(String name) {
88+
Class<Entity> entityClass = getClass(targetBasePackage, name);
89+
if (entityClass.isInterface()) {
90+
throw new RuntimeException("class " + name + " is interface!");
91+
}
92+
return entityClass;
93+
}
94+
95+
@SuppressWarnings("unchecked")
96+
public Class<Entity> classForName(String name) {
97+
try {
98+
Class target = ClassUtils.forName(name, this.getClass().getClassLoader());
99+
return target;
100+
} catch (ClassNotFoundException e) {
101+
throw new RuntimeException(e);
102+
}
103+
}
104+
105+
public String getSourceBasePackage() {
106+
return sourceBasePackage;
107+
}
108+
109+
public void setSourceBasePackage(String sourceBasePackage) {
110+
this.sourceBasePackage = sourceBasePackage;
111+
}
112+
113+
public String getTargetBasePackage() {
114+
return targetBasePackage;
115+
}
116+
117+
public void setTargetBasePackage(String targetBasePackage) {
118+
this.targetBasePackage = targetBasePackage;
119+
}
120+
121+
public Map<String, String> getMapping() {
122+
return mapping;
123+
}
124+
125+
public void setMapping(Map<String, String> mapping) {
126+
this.mapping = mapping;
127+
}
128+
}
129+
}

hsweb-starter/hsweb-spring-boot-starter/src/main/java/org/hswebframework/web/starter/HswebAutoConfiguration.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.beans.factory.annotation.Autowired;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3233
import org.springframework.context.annotation.Bean;
3334
import org.springframework.context.annotation.ComponentScan;
3435
import org.springframework.context.annotation.Configuration;
@@ -37,6 +38,9 @@
3738
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3839
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
3940

41+
import javax.validation.Validation;
42+
import javax.validation.Validator;
43+
import javax.validation.ValidatorFactory;
4044
import java.util.List;
4145

4246
/**
@@ -46,8 +50,13 @@
4650
*/
4751
@Configuration
4852
@ComponentScan("org.hswebframework.web")
53+
@EnableConfigurationProperties(EntityProperties.class)
4954
public class HswebAutoConfiguration {
5055

56+
@Autowired(required = false)
57+
private EntityProperties entityProperties;
58+
59+
5160
@Bean
5261
@Primary
5362
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(@Autowired(required = false) EntityFactory entityFactory) {
@@ -86,10 +95,18 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
8695
};
8796
}
8897

89-
@Bean
90-
@ConditionalOnMissingBean
98+
@Bean(name = "validator")
99+
@ConditionalOnMissingBean(Validator.class)
100+
public Validator validator() {
101+
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
102+
Validator validator = factory.getValidator();
103+
return validator;
104+
}
105+
106+
@Bean(name = "entityFactory")
107+
@ConditionalOnMissingBean(EntityFactory.class)
91108
public EntityFactory entityFactory() {
92-
return new MapperEntityFactory();
109+
return new MapperEntityFactory(entityProperties.createMappers());
93110
}
94111

95112
}

0 commit comments

Comments
 (0)