From b10df15e0ee8c8d781c348ca8038223dcc4873e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=B8=9C=E6=B5=B7?= <2544468477@qq.com> Date: Mon, 17 Aug 2020 20:45:50 +0800 Subject: [PATCH] config,enumtype --- config/CommandRunner.java | 33 +++++++++++++ config/CorsConfig.java | 40 +++++++++++++++ config/DefaultMvcConfig.java | 26 ++++++++++ config/DruidConfig.java | 48 ++++++++++++++++++ config/LocalDateTimeSerializerConfig.java | 29 +++++++++++ config/MyLocaleResolver.java | 32 ++++++++++++ config/PageHelperConfig.java | 30 ++++++++++++ config/Swagger2.java | 51 +++++++++++++++++++ config/ThreadPoolConfig.java | 51 +++++++++++++++++++ enumtype/Direction.java | 46 +++++++++++++++++ enumtype/PlateColor.java | 60 +++++++++++++++++++++++ 11 files changed, 446 insertions(+) create mode 100644 config/CommandRunner.java create mode 100644 config/CorsConfig.java create mode 100644 config/DefaultMvcConfig.java create mode 100644 config/DruidConfig.java create mode 100644 config/LocalDateTimeSerializerConfig.java create mode 100644 config/MyLocaleResolver.java create mode 100644 config/PageHelperConfig.java create mode 100644 config/Swagger2.java create mode 100644 config/ThreadPoolConfig.java create mode 100644 enumtype/Direction.java create mode 100644 enumtype/PlateColor.java diff --git a/config/CommandRunner.java b/config/CommandRunner.java new file mode 100644 index 0000000..ef1491f --- /dev/null +++ b/config/CommandRunner.java @@ -0,0 +1,33 @@ +package com.yuxue.config; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import lombok.extern.slf4j.Slf4j; + + +/** + * 配置自动启动浏览器 + * + */ +@Slf4j +@Component +public class CommandRunner implements CommandLineRunner { + + @Value("${server.port}") + private String port; + + @Override + public void run(String... args) { + try { + String os = System.getProperty("os.name").toLowerCase(); + if(os.contains("windows")) { + // 默认浏览器打开 + // Runtime.getRuntime().exec("cmd /c start http://localhost:" + port + "/index"); + } + } catch (Exception ex) { + ex.printStackTrace(); + log.error("打开默认浏览器异常", ex); + } + } +} diff --git a/config/CorsConfig.java b/config/CorsConfig.java new file mode 100644 index 0000000..c1babee --- /dev/null +++ b/config/CorsConfig.java @@ -0,0 +1,40 @@ +package com.yuxue.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * 跨域通配 + * 支持 @CrossOrigin 注解局部声明 + * @author yuxue + * @date 2018-09-07 + */ +@Configuration +public class CorsConfig { + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + /** * 至少需要addMapping *** */ + registry + .addMapping("/**") + .allowedOrigins("*") + .allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS", "HEAD") + .allowedHeaders("Content-Type", "X-Requested-With", "accept", "Authorization", "Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers") + .allowCredentials(true)//是否带上cookie + .maxAge(3600) + .exposedHeaders( + "access-control-allow-headers", + "access-control-allow-methods", + "access-control-allow-origin", + "access-Control-allow-credentials", + "access-control-max-age", + "X-Frame-Options"); + } + }; + } +} diff --git a/config/DefaultMvcConfig.java b/config/DefaultMvcConfig.java new file mode 100644 index 0000000..d62aa5e --- /dev/null +++ b/config/DefaultMvcConfig.java @@ -0,0 +1,26 @@ +package com.yuxue.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; + + + +/** + * 自定义配置 + * @author yuxue + * @date 2019-06-13 + */ +@Configuration +public class DefaultMvcConfig { + + /** + * 国际化配置 + * 设置区域信息 + * @return + */ + @Bean + public LocaleResolver localeResolver(){ + return new MyLocaleResolver(); + } +} diff --git a/config/DruidConfig.java b/config/DruidConfig.java new file mode 100644 index 0000000..0378347 --- /dev/null +++ b/config/DruidConfig.java @@ -0,0 +1,48 @@ +package com.yuxue.config; + +import com.alibaba.druid.filter.Filter; +import com.alibaba.druid.filter.stat.StatFilter; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.http.StatViewServlet; +import com.google.common.collect.Lists; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * druid连接池的配置,配置如何处理慢sql, + */ +@Configuration +public class DruidConfig { + + // 这个注解读取配置文件前缀为prefix的配置,将外部的配置文件与这里绑定 + // 容器的开启与关闭 + @ConfigurationProperties(prefix = "spring.druid") + @Bean(initMethod = "init", destroyMethod = "close") + public DruidDataSource dataSource() { + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setProxyFilters(Lists.newArrayList(statFilter())); + return dataSource; + } + + // bean注解,成为spring的bean,利用filter将慢sql的日志打印出来 + //@Bean + public Filter statFilter() { + StatFilter statFilter = new StatFilter(); + // 多长时间定义为慢sql,这里定义为5s + statFilter.setSlowSqlMillis(5000); + // 是否打印出慢日志 + statFilter.setLogSlowSql(true); + // 是否将日志合并起来 + statFilter.setMergeSql(true); + return statFilter; + } + + // 这是配置druid的监控 + @Bean + public ServletRegistrationBean servletRegistrationBean() { + return new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); + } + +} diff --git a/config/LocalDateTimeSerializerConfig.java b/config/LocalDateTimeSerializerConfig.java new file mode 100644 index 0000000..8e6fbde --- /dev/null +++ b/config/LocalDateTimeSerializerConfig.java @@ -0,0 +1,29 @@ +package com.yuxue.config; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; + +@Configuration +public class LocalDateTimeSerializerConfig { + + @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") + private String pattern; + + @Bean + public LocalDateTimeSerializer localDateTimeDeserializer() { + return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); + } + + @Bean + public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { + return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); + } + +} \ No newline at end of file diff --git a/config/MyLocaleResolver.java b/config/MyLocaleResolver.java new file mode 100644 index 0000000..0d4cedb --- /dev/null +++ b/config/MyLocaleResolver.java @@ -0,0 +1,32 @@ +package com.yuxue.config; + +import org.springframework.util.StringUtils; +import org.springframework.web.servlet.LocaleResolver; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Locale; + +/** + * 配置多语言 + * @author yuxue + * @date 2019-06-13 + */ +public class MyLocaleResolver implements LocaleResolver { + + @Override + public Locale resolveLocale(HttpServletRequest request) { + String l = request.getParameter("i18n"); + Locale locale = Locale.getDefault(); + if(!StringUtils.isEmpty(l)){ + String[] split = l.split("_"); + locale = new Locale(split[0],split[1]); + } + return locale; + } + + @Override + public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { + + } +} diff --git a/config/PageHelperConfig.java b/config/PageHelperConfig.java new file mode 100644 index 0000000..c7e24f7 --- /dev/null +++ b/config/PageHelperConfig.java @@ -0,0 +1,30 @@ +package com.yuxue.config; + +import com.github.pagehelper.PageInterceptor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Properties; + + +/** + * 分页查询控制 + * @author yuxue + * @date 2018-09-07 + */ +@Configuration +public class PageHelperConfig { + + @Value("${pagehelper.helperDialect}") + private String helperDialect; + + @Bean + public PageInterceptor pageInterceptor() { + PageInterceptor pageInterceptor = new PageInterceptor(); + Properties properties = new Properties(); + properties.setProperty("helperDialect", helperDialect); + pageInterceptor.setProperties(properties); + return pageInterceptor; + } +} diff --git a/config/Swagger2.java b/config/Swagger2.java new file mode 100644 index 0000000..d9eca1c --- /dev/null +++ b/config/Swagger2.java @@ -0,0 +1,51 @@ +package com.yuxue.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RestController; + +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + + +/** + * 在swagger-annotations jar包中 1.5.X版本以上, 注解 io.swagger.annotations.API 中的description被废弃了。 + * 新的swagger组件中使用了新的方法来对Web api 进行分组。原来使用 description ,默认一个Controller类中包含的方法构成一 个api分组。 + * 现在使用tag,可以更加方便的分组。 + * 比如把两个Controller类里的方法划分成同一个分组。tag的key用来区分不同的分组。tag的value用做分组的描述。 + * @ApiOperation 中value是api的简要说明,在界面api 链接的右侧,少于120个字符。 + * @ApiOperation 中notes是api的详细说明,需要点开api 链接才能看到。 + * @ApiOperation 中 produces 用来标记api返回值的具体类型。这里是json格式,utf8编码。 + */ +/** + * 集成swagger2 接口管理文档 + * @author yuxue + * @date 2018-09-07 + */ +@Configuration +@EnableSwagger2 +public class Swagger2 { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("Image Recognition API") + .description("图像识别技术") + .version("1.0.0") + .build(); + } +} diff --git a/config/ThreadPoolConfig.java b/config/ThreadPoolConfig.java new file mode 100644 index 0000000..ac18710 --- /dev/null +++ b/config/ThreadPoolConfig.java @@ -0,0 +1,51 @@ +package com.yuxue.config; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +@ComponentScan("com.yuxue.auth.service.impl") +@EnableAsync +public class ThreadPoolConfig implements AsyncConfigurer { + + @Bean(name = "taskExecutor") + public TaskExecutor taskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + + // 设置核心线程数 + executor.setCorePoolSize(4); + // 设置最大线程数 + executor.setMaxPoolSize(8); + // 设置队列容量 + executor.setQueueCapacity(100); + // 设置线程活跃时间(秒) + executor.setKeepAliveSeconds(60); + // 设置默认线程名称 + executor.setThreadNamePrefix("localThread:"); + // 设置拒绝策略 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + // 等待所有任务结束后再关闭线程池 + executor.setWaitForTasksToCompleteOnShutdown(true); + return executor; + } + + @Override + public Executor getAsyncExecutor() { + return taskExecutor(); + } + + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return null; + } + +} diff --git a/enumtype/Direction.java b/enumtype/Direction.java new file mode 100644 index 0000000..e17914f --- /dev/null +++ b/enumtype/Direction.java @@ -0,0 +1,46 @@ +package com.yuxue.enumtype; + +public enum Direction { + + VERTICAL("VERTICAL","垂直"), + HORIZONTAL("HORIZONTAL","水平"), + UNKNOWN("UNKNOWN","未知"); + + public final String code; + public final String desc; + + Direction(String code, String desc) { + this.code = code; + this.desc = desc; + } + + public static String getDesc(String code) { + Direction[] enums = values(); + for (Direction type : enums) { + if (type.code().equals(code)) { + return type.desc(); + } + } + return null; + } + + public static String getCode(String desc) { + Direction[] enums = values(); + for (Direction type : enums) { + if (type.desc().equals(desc)) { + return type.code(); + } + } + return null; + } + + + public String code() { + return this.code; + } + + public String desc() { + return this.desc; + } + +} diff --git a/enumtype/PlateColor.java b/enumtype/PlateColor.java new file mode 100644 index 0000000..f5fa607 --- /dev/null +++ b/enumtype/PlateColor.java @@ -0,0 +1,60 @@ +package com.yuxue.enumtype; + + +/** + * 车牌颜色 + * @author yuxue + * @date 2020-05-08 12:38 + */ +public enum PlateColor { + + BLUE("BLUE","蓝牌", 100, 130), + GREEN("GREEN","绿牌", 38, 100), + YELLOW("YELLOW","黄牌", 15, 40), + BLACK("BLACK","黑牌", 0, 180), + UNKNOWN("UNKNOWN","未知", 0, 0); + + public final String code; + public final String desc; + + // opencv颜色识别的HSV中各个颜色所对应的H的范围: Orange 0-22 Yellow 22- 38 Green 38-75 Blue 75-130 + public final int minH; + public final int maxH; + + PlateColor(String code, String desc, int minH, int maxH) { + this.code = code; + this.desc = desc; + this.minH = minH; + this.maxH = maxH; + } + + public static String getDesc(String code) { + PlateColor[] enums = values(); + for (PlateColor type : enums) { + if (type.code().equals(code)) { + return type.desc(); + } + } + return null; + } + + public static String getCode(String desc) { + PlateColor[] enums = values(); + for (PlateColor type : enums) { + if (type.desc().equals(desc)) { + return type.code(); + } + } + return null; + } + + + public String code() { + return this.code; + } + + public String desc() { + return this.desc; + } + +}