mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-22 00:32:47 +08:00
增加bean模块
This commit is contained in:
30
hsweb-commons/hsweb-commons-bean/pom.xml
Normal file
30
hsweb-commons/hsweb-commons-bean/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>hsweb-commons</artifactId>
|
||||
<groupId>org.hswebframework.web</groupId>
|
||||
<version>3.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>hsweb-commons-bean</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hswebframework.web</groupId>
|
||||
<artifactId>hsweb-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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 <T> 对象类型
|
||||
* @return 原始对象
|
||||
* @see FastBeanCopier
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
default <T extends Bean> T copyFrom(Object from, String... ignore) {
|
||||
return (T) FastBeanCopier.copy(from, this, ignore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象的属性复制到指定的对象中
|
||||
*
|
||||
* @param to 要复制到的对象
|
||||
* @param ignore 不复制的字段
|
||||
* @param <T> 对象类型
|
||||
* @return 复制后的对象
|
||||
* @see FastBeanCopier
|
||||
*/
|
||||
default <T> T copyTo(T to, String... ignore) {
|
||||
return FastBeanCopier.copy(this, to, ignore);
|
||||
}
|
||||
}
|
||||
@@ -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> T tryValidate(T bean, Class... group) {
|
||||
Set<ConstraintViolation<T>> violations = getValidator().validate(bean, group);
|
||||
if (!violations.isEmpty()) {
|
||||
SimpleValidateResults results = new SimpleValidateResults();
|
||||
for (ConstraintViolation<T> violation : violations) {
|
||||
results.addResult(violation.getPropertyPath().toString(), violation.getMessage());
|
||||
}
|
||||
throw new ValidationException(results);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 <T> 当前对象类型
|
||||
* @return 当前对象
|
||||
*/
|
||||
default <T extends ValidateBean> T tryValidate(Class... group) {
|
||||
BeanValidator.tryValidate(this, group);
|
||||
return (T) this;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.hswebframework.web.template.starter.ModuleAutoConfiguration
|
||||
org.hswebframework.web.commons.bean.BeanValidatorAutoConfiguration
|
||||
@@ -1,18 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>hsweb-system-module</artifactId>
|
||||
<groupId>org.hswebframework.web</groupId>
|
||||
<version>3.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>hsweb-system-module-dao</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>hsweb-system-module-dao-api</module>
|
||||
<module>hsweb-system-module-dao-mybatis</module>
|
||||
</modules>
|
||||
</project>
|
||||
Reference in New Issue
Block a user