config,enumtype

master
黄东海 4 years ago
parent 6b3d62878d
commit b10df15e0e

@ -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);
}
}
}

@ -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");
}
};
}
}

@ -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();
}
}

@ -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/*");
}
}

@ -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());
}
}

@ -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) {
}
}

@ -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;
}
}

@ -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便
* Controllertagkeytagvalue
* @ApiOperation valueapiapi 120
* @ApiOperation notesapiapi
* @ApiOperation produces apijsonutf8
*/
/**
* 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();
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}
Loading…
Cancel
Save