常用
异常
- UnsupportedOperationException
日志
log.info(LogUtils.gen("message", "key1", value1, "key2", value2)); log.info(LogUtils.gen("getUser", ImmutableMap.<String, String> builder().put("token", token).put("userId", userId).build()));
对象克隆
// 一般是从parent克隆出child BeanUtils.copyProperties(parent, child);
线程
new Thread(new Runnable() { @Override public void run() { // code here } } }).start();
模板函数
public <T> void test(T t1) {} // 函数定义 test(new ClassA()); // 调用
回调函数(Java8的功能)
public void callbackUsage(Function<String, String> fn) { fn.apply(“123”); } this.callbackUsage(new Function<String, String>() { @Override public String apply(String t) { return null; } });
注解类获取
this.getClass().isAnnotationPresent(BizApi.class); // 获取所有引用的判断:BizApi是annotation
bean获取
this.service = this.context.getBean(IUserAccountService.class);
for (Map.Entry<String, Object> entry : this.context.getBeansWithAnnotation(BizApi.class).entrySet()) {
this.apiMap.put(entry.getKey(), (IBizApi) entry.getValue());
}
文件系统
- 组合路径
Path path = Paths.get("path1", "path2", "fileName"); File file = path.toFile();