1. Map实现自定义缓存
1.1 创建定时器线程池
static class FactoryClass implements ThreadFactory{
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "SegmentScheduledExecutorThread");
t.setDaemon(true);
return t;
}
}
private static final ScheduledExecutorService ScheduledService =
Executors.newSingleThreadScheduledExecutor(new FactoryClass());
1.2 创建map并实现put和get方法
private final static Map<String, Entity> MAP = new HashMap<>(16);
/**
* 添加缓存
*
* @param key 键
* @param data 值
* @param expire 过期时间,单位:毫秒, 0表示无限长
*/
public synchronized static void put(String key, Object data, long expire) {
//清除原键值对
CacheUtils.remove(key);
//设置过期时间
if (expire > 0) {
Future future = ScheduledService.schedule(new Runnable() {
@Override
public void run() {
//过期后清除该键值对
synchronized (CacheUtils.class) {
MAP.remove(key);
}
}
}, expire, TimeUnit.MILLISECONDS);
MAP.put(key, new Entity(data, future));
} else {
//不设置过期时间
MAP.put(key, new Entity(data, null));
}
}
/**
* 读取缓存
*
* @param key 键
* @return
*/
public synchronized static <T> T get(String key) {
Entity entity = MAP.get(key);
return entity == null ? null : (T) entity.value;
}
1.3创建remove方法,移除过期的键值对
/**
* 清除缓存
*
* @param key 键
* @return
*/
public synchronized static <T> T remove(String key) {
//清除原缓存数据
Entity entity = MAP.remove(key);
if (entity == null) {
return null;
}
//清除原键值对定时器
if (entity.future != null) {
entity.future.cancel(true);
}
return (T) entity.value;
}
1.4 使用到的内部实体类
/**
* 缓存实体类
*/
static class Entity {
/**
* 键值对的value
*/
public Object value;
/**
* 定时器Future
*/
public Future future;
public Entity(Object value, Future future) {
this.value = value;
this.future = future;
}
}
2. 全局异常控制
Spring处理异常的方式有三种
- @ExceptionHandler:统一处理某一类异常,从而能够减少代码重复率和复杂度
- @ControllerAdvice:异常集中处理,更好的使业务逻辑与异常处理剥离开,其是对controller层进行拦截
- @ResponseStatus:可以将某种异常映射为HTTP状态码
2.1 @ExceptionHandler
源码
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionHandler {
Class<? extends Throwable>[] value() default {};
}
作用:该注解作用对象为方法,并且在运行时有效,value()可以指定异常类。由该注解注释的方法可以具有灵活的输入参数(详细参见Spring API):
- 异常参数:包括一般的异常或特定的异常(即自定义异常),如果注解没有指定异常类,会默认进行映射。
- 请求或响应对象 (Servlet API or Portlet API): 你可以选择不同的类型,如ServletRequest/HttpServletRequest或PortleRequest/ActionRequest/RenderRequest。
- Session对象(Servlet API or Portlet API): HttpSession或PortletSession。
WebRequest或NativeWebRequest - Locale
- InputStream/Reader
- OutputStream/Writer
- Model
方法返回值可以为:
ModelAndView对象
- Model对象
- Map对象
- View对象
- String对象
- @ResponseBody、HttpEntity<?>或ResponseEntity<?>,以及void
2.2 @ControllerAdvice
源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<?>[] assignableTypes() default {};
Class<? extends Annotation>[] annotations() default {};
}
作用:该注解作用对象为TYPE,包括类、接口和枚举等,在运行时有效,并且可以通过Spring扫描为bean组件。其可以包含由@ExceptionHandler、@InitBinder 和@ModelAttribute标注的方法,可以处理多个Controller类,这样所有控制器的异常可以在一个地方进行处理。
2.3 实例
异常类
public class CustomGenericException extends RuntimeException{
private static final long serialVersionUID = 1L;
private String errCode;
private String errMsg;
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public CustomGenericException(String errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
}
控制器
@Controller
@RequestMapping("/exception")
public class ExceptionController {
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
public ModelAndView getPages(@PathVariable(value = "type") String type) throws Exception{
if ("error".equals(type)) {
// 由handleCustomException处理
throw new CustomGenericException("E888", "This is custom message");
} else if ("io-error".equals(type)) {
// 由handleAllException处理
throw new IOException();
} else {
return new ModelAndView("index").addObject("msg", type);
}
}
}
异常处理类
@ControllerAdvice
public class ExceptionsHandler {
@ExceptionHandler(CustomGenericException.class)//可以直接写@ExceptionHandler,不指明异常类,会自动映射
public ModelAndView customGenericExceptionHnadler(CustomGenericException exception){ //还可以声明接收其他任意参数
ModelAndView modelAndView = new ModelAndView("generic_error");
modelAndView.addObject("errCode",exception.getErrCode());
modelAndView.addObject("errMsg",exception.getErrMsg());
return modelAndView;
}
@ExceptionHandler(Exception.class)//可以直接写@EceptionHandler,IOExeption继承于Exception
public ModelAndView allExceptionHandler(Exception exception){
ModelAndView modelAndView = new ModelAndView("generic_error");
modelAndView.addObject("errMsg", "this is Exception.class");
return modelAndView;
}
}
3. 小知识点
3.1 Joiner的用法
Joiner类专门用来连接String
如一个String数组,通过StringBuilder可以创建String“a”,“b”,“c”
Joiner提供了这一类的功能
Joiner joiner = Joiner.on(":");
assertEquals("a","b","c",joiner.join(new String[]{"a","b","c"}));
Joiner.join还提供了参数为iterrable的overload形式,可以传各种List和set
被连接String里面要过滤null
Joiner joiner = Joiner.on(";").skipNulls();
assertEquals("a;c",joiner.join(new String[]{"a",null,"c"}))
或者对null进行替换操作
Joiner joiner = Joiner.on(";").useForNull("!");
assertEquals("a;!;c", joiner.join(new String[]{"a",null,"c"}));
joiner还提供了appendTo函数,对传入的StringBuilder进行处理
Joiner joiner = Joiner.on(";");
StringBuilder ab = new StringBuilder("start:");
assertEquals("start: a;b;c",joiner.appendTo(ab,new String[]{"a","b","c"}).toString)
Joiner提供了Map的join功能
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
MapJoiner joiner = Joiner.on(";").withKeyValueSeparator("->");
assertEquals("1->a;2->b", joiner.join(map));
joiner类是immutable的,换言之,它是线程安全的,可以把它static finall来用没问题
最好不要!!!
// Bad! Do not do this!
Joiner joiner = Joiner.on(',');
joiner.skipNulls(); // does nothing!
return joiner.join("wrong", null, "wrong");
其他常用的字符串连接方式
StringBuilder
StringBuilder strBur = new StringBuilder();String result = list.stream().collect(Collectors.joining("_"));
list.forEach(val -> {
strBur.append(val).append("#");
});
strBur.toString();
Java8Steam
String result = list.stream().collect(Collectors.joining("_"));