No.8
public static void main(String[] args) {
//创建一个工作队列
BlockingQueue weekQueue = new LinkedBlockingQueue<>(3);
//创建一个线程创建工程
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
//创建一个核心线程5 最大线程10 空闲线程等待1分钟的线程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 1,
TimeUnit.MINUTES, weekQueue, threadFactory, new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 10; i++) {
DemoTask demoTask = new DemoTask(i);
try{
threadPoolExecutor.execute(demoTask);
}catch (Exception exception){
exception.printStackTrace();
threadPoolExecutor.shutdown();
}
}
}
JavaJava线程池的创建
No.7
private static final Logger logger = LoggerFactory.getLogger(LoggerDemo.class);
Java在Java中创建一个日志打印对象
No.6
logging:
level:
com.examlple.test: debug
xmlspringboot项目配置日志级别
No.5
@Data
public class BaseResponse{
public static final int SUCCESS_CODE = 0;
public static final int SYS_ERROR_CODE = 999;
/**
* 请求处理状态
*/
private Integer errorCode;
/**
* 提示信息
*/
private String message;
/**
* 错误异常信息
*/
private String errorMessage;
/**
* 返回数据
*/
private T data;
public static BaseResponse response(Integer code, String message, String errorMessage, R data){
BaseResponse response = new BaseResponse<>();
response.setErrorCode(code);
response.setMessage(message);
response.setErrorMessage(errorMessage);
response.setData(data);
return response;
}
public static BaseResponse success(R data){
return BaseResponse.response(BaseResponse.SUCCESS_CODE, "success", null, data);
}
public static BaseResponse success(){
return success(null);
}
public static BaseResponse fail(String errorMessage){
return BaseResponse.response(BaseResponse.SYS_ERROR_CODE, null, errorMessage, null);
}
public static BaseResponse fail(String message, String errorMessage){
return BaseResponse.response(BaseResponse.SYS_ERROR_CODE, message, errorMessage, null);
}
public static BaseResponse fail(Integer errorCode, String errorMessage){
return BaseResponse.response(errorCode, null, errorMessage, null);
}
public static BaseResponse fail(){
return fail(null);
}
}
Java一个接口统一返回对象
No.4
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext ;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static T getBean(String beanName, Class clazz){
return applicationContext.getBean(beanName, clazz);
}
public static T getBean(Class clazz){
return applicationContext.getBean(clazz);
}
}
JavaSpringBean操作类
No.3
public static HttpServletRequest request(){
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (Objects.isNull(attributes)) {
throw new RuntimeException("RequestAttributes is empty!");
}
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) attributes;
return requestAttributes.getRequest();
}
JavaSpring获取请求Request
No.2
public class JsonUtils {
public static final ObjectMapper OBJECT_MAPPER;
static {
OBJECT_MAPPER = new ObjectMapper();
OBJECT_MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
OBJECT_MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
OBJECT_MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
OBJECT_MAPPER.findAndRegisterModules();
}
public static String toJsonString(Object object){
try {
return OBJECT_MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static T parse(String jsonString, Class clazz){
try {
return OBJECT_MAPPER.readValue(jsonString, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static List parseArray(String countInfo, Class clazz) {
try {
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(List.class, clazz);
return OBJECT_MAPPER.readValue(countInfo, javaType);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static List parseArrayNull(String countInfo, Class clazz) {
try {
if (StringUtils.isBlank(countInfo)){
return Lists.newArrayList();
}
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(List.class, clazz);
return OBJECT_MAPPER.readValue(countInfo, javaType);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Java一个Jackson的json工具类
No.1
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(false);
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedMethod("GET");
corsConfiguration.addAllowedMethod("POST");
corsConfiguration.addAllowedMethod("PUT");
corsConfiguration.addAllowedMethod("DELETE");
corsConfiguration.addAllowedMethod("OPTIONS");
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(source);
}
}
JavaSpring跨域统一处理代码