开源mall学习

 https://github.com/macrozheng/mall

学习知识点

1、Spring Security

 

2、@Aspect

 

3、logstash

 

4、 es

crud

templete

 

5、@Validated

ConstraintValidator 
BindingResult

1、定义注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Constraint(validatedBy = FlagValidatorClass.class)
public @interface FlagValidator {
    String[] value() default {};

    String message() default "flag is not found";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}


2、定义校验器
public class FlagValidatorClass implements ConstraintValidator<FlagValidator,Integer> {
    private String[] values;
    @Override
    public void initialize(FlagValidator flagValidator) {
        this.values = flagValidator.value();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        boolean isValid = false;
        if(value==null){
            //当状态为空时使用默认值
            return true;
        }
        for(int i=0;i<values.length;i++){
            if(values[i].equals(String.valueOf(value))){
                isValid = true;
                break;
            }
        }
        return isValid;
    }
}


3、model定义
@FlagValidator(value = {"0","1"},message = "状态只能为0或1")
private Integer showStatus;

4、控制器controller
@ApiOperation("创建商品")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public Object create(@RequestBody PmsProductParam productParam, BindingResult bindingResult) {
int count = productService.create(productParam);
if (count > 0) {
return new CommonResult().success(count);
} else {
return new CommonResult().failed();
}
}

5、错误处理 @Aspect @Component @Order(2) public class BindingResultAspect { @Pointcut("execution(public * com.macro.mall.controller.*.*(..))") public void BindingResult() { } @Around("BindingResult()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { Object[] args = joinPoint.getArgs(); for (Object arg : args) { if (arg instanceof BindingResult) { BindingResult result = (BindingResult) arg; if (result.hasErrors()) { return new CommonResult().validateFailed(result); } } } return joinPoint.proceed(); } }

 


 

 

 

6、Swagger

@Configuration
@EnableSwagger2

@ConditionalOnExpression("'${swagger.enable}' == 'true'")
public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.macro.mall.search.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("mall搜索系统") .description("mall搜索模块") .contact("macro") .version("1.0") .build(); } }

 

 

7、线程池初始化

 

@Configuration
public class ThreadConfig {

    @Value("${concurrent.core.size}")
    private int coreSize ;

    @Value("${concurrent.max.size}")
    private int maxSize ;

    @Value("${concurrent.blockqueue.size}")
    private int blockQueueSize;



    @Bean(value = "concurrentTestThread")
    public ExecutorService build(){
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("concurrent-thread-%d").build();
        ThreadPoolExecutor executorServicePool = new ThreadPoolExecutor(coreSize, maxSize, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(blockQueueSize), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return executorServicePool ;
    }

}


@Resource(name = "concurrentTestThread")
private ExecutorService executorService;
 

 

 

ref:

https://www.cnblogs.com/softidea/p/5991897.html

https://www.cnblogs.com/cjsblog/p/9459781.html

posted @ 2019-03-28 17:20  人在江湖之诗和远方  阅读(805)  评论(0编辑  收藏  举报