From a349710338a7f7e2cb1d233e6d09eb698a48cfdf Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 17 Apr 2018 18:26:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0bean=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hsweb-commons/hsweb-commons-bean/pom.xml | 30 ++++++++++++ .../hswebframework/web/commons/bean/Bean.java | 36 +++++++++++++++ .../web/commons/bean/BeanValidator.java | 46 +++++++++++++++++++ .../bean/BeanValidatorAutoConfiguration.java | 38 +++++++++++++++ .../web/commons/bean/ValidateBean.java | 22 +++++++++ .../main/resources/META-INF/spring.factories | 2 +- .../hsweb-system-module-dao/pom.xml | 18 -------- 7 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 hsweb-commons/hsweb-commons-bean/pom.xml create mode 100644 hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/Bean.java create mode 100644 hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidator.java create mode 100644 hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidatorAutoConfiguration.java create mode 100644 hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/ValidateBean.java rename {hsweb-system/hsweb-system-module/hsweb-system-module-starter => hsweb-commons/hsweb-commons-bean}/src/main/resources/META-INF/spring.factories (55%) delete mode 100644 hsweb-system/hsweb-system-module/hsweb-system-module-dao/pom.xml diff --git a/hsweb-commons/hsweb-commons-bean/pom.xml b/hsweb-commons/hsweb-commons-bean/pom.xml new file mode 100644 index 000000000..ba99e558d --- /dev/null +++ b/hsweb-commons/hsweb-commons-bean/pom.xml @@ -0,0 +1,30 @@ + + + + hsweb-commons + org.hswebframework.web + 3.0-SNAPSHOT + + 4.0.0 + + hsweb-commons-bean + + + + org.hswebframework.web + hsweb-core + ${project.version} + + + org.hibernate + hibernate-validator + + + org.springframework.boot + spring-boot-starter + true + + + \ No newline at end of file diff --git a/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/Bean.java b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/Bean.java new file mode 100644 index 000000000..3f3e1d849 --- /dev/null +++ b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/Bean.java @@ -0,0 +1,36 @@ +package org.hswebframework.web.commons.bean; + +import org.hswebframework.web.bean.FastBeanCopier; + +/** + * @author zhouhao + * @since 3.0 + */ +public interface Bean { + /** + * 从指定的对象中复制属性到本对象 + * + * @param from 要复制的对象 + * @param ignore 不复制的字段 + * @param 对象类型 + * @return 原始对象 + * @see FastBeanCopier + */ + @SuppressWarnings("all") + default T copyFrom(Object from, String... ignore) { + return (T) FastBeanCopier.copy(from, this, ignore); + } + + /** + * 将对象的属性复制到指定的对象中 + * + * @param to 要复制到的对象 + * @param ignore 不复制的字段 + * @param 对象类型 + * @return 复制后的对象 + * @see FastBeanCopier + */ + default T copyTo(T to, String... ignore) { + return FastBeanCopier.copy(this, to, ignore); + } +} diff --git a/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidator.java b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidator.java new file mode 100644 index 000000000..b166e4b9e --- /dev/null +++ b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidator.java @@ -0,0 +1,46 @@ +package org.hswebframework.web.commons.bean; + +import lombok.extern.slf4j.Slf4j; +import org.hswebframework.web.validate.SimpleValidateResults; +import org.hswebframework.web.validate.ValidationException; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import java.util.Set; + +/** + * @author zhouhao + * @since 3.0 + */ +@Slf4j +public final class BeanValidator { + + private BeanValidator() { + } + + static volatile Validator validator; + + public static Validator getValidator() { + if (validator == null) { + synchronized (BeanValidator.class) { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + return validator = factory.getValidator(); + } + } + return validator; + } + + public static T tryValidate(T bean, Class... group) { + Set> violations = getValidator().validate(bean, group); + if (!violations.isEmpty()) { + SimpleValidateResults results = new SimpleValidateResults(); + for (ConstraintViolation violation : violations) { + results.addResult(violation.getPropertyPath().toString(), violation.getMessage()); + } + throw new ValidationException(results); + } + return bean; + } +} diff --git a/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidatorAutoConfiguration.java b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidatorAutoConfiguration.java new file mode 100644 index 000000000..47f20afaa --- /dev/null +++ b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/BeanValidatorAutoConfiguration.java @@ -0,0 +1,38 @@ +package org.hswebframework.web.commons.bean; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +/** + * @author zhouhao + * @since 3.0 + */ +@Configuration +public class BeanValidatorAutoConfiguration implements BeanPostProcessor { + @Bean(name = "validator") + @ConditionalOnMissingBean(Validator.class) + public Validator validator() { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + return factory.getValidator(); + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof Validator) { + BeanValidator.validator = ((Validator) bean); + } + return bean; + } +} diff --git a/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/ValidateBean.java b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/ValidateBean.java new file mode 100644 index 000000000..d504f4821 --- /dev/null +++ b/hsweb-commons/hsweb-commons-bean/src/main/java/org/hswebframework/web/commons/bean/ValidateBean.java @@ -0,0 +1,22 @@ +package org.hswebframework.web.commons.bean; + +/** + * 支持验证的bean + * + * @author zhouhao + * @since 3.0 + */ +public interface ValidateBean extends Bean { + + /** + * 尝试验证此bean,如果验证未通过,将抛出{@link org.hswebframework.web.validate.ValidationException} + * + * @param group 验证分组 + * @param 当前对象类型 + * @return 当前对象 + */ + default T tryValidate(Class... group) { + BeanValidator.tryValidate(this, group); + return (T) this; + } +} diff --git a/hsweb-system/hsweb-system-module/hsweb-system-module-starter/src/main/resources/META-INF/spring.factories b/hsweb-commons/hsweb-commons-bean/src/main/resources/META-INF/spring.factories similarity index 55% rename from hsweb-system/hsweb-system-module/hsweb-system-module-starter/src/main/resources/META-INF/spring.factories rename to hsweb-commons/hsweb-commons-bean/src/main/resources/META-INF/spring.factories index d34b02a74..0bdb7216a 100644 --- a/hsweb-system/hsweb-system-module/hsweb-system-module-starter/src/main/resources/META-INF/spring.factories +++ b/hsweb-commons/hsweb-commons-bean/src/main/resources/META-INF/spring.factories @@ -1,3 +1,3 @@ # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.hswebframework.web.template.starter.ModuleAutoConfiguration \ No newline at end of file +org.hswebframework.web.commons.bean.BeanValidatorAutoConfiguration \ No newline at end of file diff --git a/hsweb-system/hsweb-system-module/hsweb-system-module-dao/pom.xml b/hsweb-system/hsweb-system-module/hsweb-system-module-dao/pom.xml deleted file mode 100644 index ab33714e5..000000000 --- a/hsweb-system/hsweb-system-module/hsweb-system-module-dao/pom.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - hsweb-system-module - org.hswebframework.web - 3.0-SNAPSHOT - - 4.0.0 - - hsweb-system-module-dao - pom - - hsweb-system-module-dao-api - hsweb-system-module-dao-mybatis - - \ No newline at end of file