优化对常用接口的默认实例化

This commit is contained in:
zhouhao
2018-06-04 14:39:38 +08:00
parent 0d1cadcc42
commit fd9dfe097f
2 changed files with 15 additions and 6 deletions

View File

@@ -35,10 +35,10 @@ import java.util.function.Supplier;
* @since 3.0
*/
@SuppressWarnings("unchecked")
public class MapperEntityFactory implements EntityFactory ,BeanFactory {
private Map<Class, Mapper> realTypeMapper = new HashMap<>();
private Logger logger = LoggerFactory.getLogger(this.getClass());
private Map<String, PropertyCopier> copierCache = new HashMap<>();
public class MapperEntityFactory implements EntityFactory, BeanFactory {
private Map<Class, Mapper> realTypeMapper = new HashMap<>();
private Logger logger = LoggerFactory.getLogger(this.getClass());
private Map<String, PropertyCopier> copierCache = new HashMap<>();
private static final DefaultMapperFactory DEFAULT_MAPPER_FACTORY = clazz -> {
String simpleClassName = clazz.getPackage().getName().concat(".Simple").concat(clazz.getSimpleName());
@@ -159,6 +159,15 @@ public class MapperEntityFactory implements EntityFactory ,BeanFactory {
if (defaultClass != null) {
return newInstance(defaultClass);
}
if (Map.class == beanClass) {
return (T) new HashMap<>();
}
if (List.class == beanClass) {
return (T) new ArrayList<>();
}
if (Set.class == beanClass) {
return (T) new HashSet<>();
}
throw new NotFoundException("can't create instance for " + beanClass);
}
@@ -190,7 +199,7 @@ public class MapperEntityFactory implements EntityFactory ,BeanFactory {
}
public static class Mapper<T> {
Class<T> target;
Class<T> target;
Supplier<T> instanceGetter;
public Mapper(Class<T> target, Supplier<T> instanceGetter) {

View File

@@ -59,4 +59,4 @@ public class User {
注意: 1,2的功能由`hsweb-commons-dao`模块去实现,如果你不没有使用hsweb自带的dao实现,可能无法使用此功能.
所有的字典都会注册到:`DictDefineRepository`,可通过此类去获取字典,以提供给前端或者其他地方使用.
所有的字典都会注册到:`DictDefineRepository`,可通过此类去获取字典,以提供给前端或者其他地方使用.