1.根据方法名,获取类的对应的方法

        Method changeSessionIdMethod = ReflectionUtils.findMethod(HttpServletRequest.class, "changeSessionId");
if(changeSessionIdMethod == null) {
throw new IllegalStateException("HttpServletRequest.changeSessionId is undefined. Are you using a Servlet 3.1+ environment?");
}
System.out.println(changeSessionIdMethod);

2.休眠当前线程

TimeUnit.MILLISECONDS.sleep(1000);

3. 替换配置文件中特殊的变量将变量中含有 @{test.test} 将变量 test.test 替换为系统中的其他值。

private static final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("@{", "}", ":", false);

    private static final PlaceholderResolver placeholderResolver=new PlaceholderResolver(){
@Override
public String resolvePlaceholder(String key) { return System.getProperty(key);
} };

4.获取对象的方法名称。dubbo 获取dubbo接口里的方法

String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();

5. {0} 字符串格式化。

        String template="test1={0},test2={1},test3={2}";
Object[] objects=new Object[]{0,1,"test2"};
String emailContent = MessageFormat.format(template, objects);
System.out.println(emailContent);

6.spring HiddenHttpMethodFilter 将http里post方法转化为参数里的方法

    @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException { HttpServletRequest requestToUse = request; if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
requestToUse = new HttpMethodRequestWrapper(request, paramValue);
}
} filterChain.doFilter(requestToUse, response);
}

7.判断classpath 中是否存在某个类   原理:先判断是否是基础类型, 再 根据  ClassLoader clToUse = classLoader;          (clToUse != null ? clToUse.loadClass(name) : Class.forName(name))

org.springframework.util.ClassUtils

        if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
for (String className : WEB_ENVIRONMENT_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}

9.获取classpath下的文件,和 jar包里的文件。

    public static void main(String[] args) throws Exception {
Enumeration<URL> urls = ClassLoader.getSystemResources("META-INF/services/ch.qos.logback.classic.spi.Configurator");
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
System.out.println(url.getPath());
}
}
// 从url 读取文件内容
BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "utf-8"));  

10.向目录里写文件,目录和文件不能直接用字符串链接,如目录为:.,文件名为:test.log,直接链接为.log会出错。

public class FileUtil {

    public static File getFile(String path,String fileName) throws IOException{
File directory=new File(path);
String filePath=directory.getCanonicalPath();
return new File(filePath+"/"+fileName);
}
}

11.字符串按字节长度截取

    public static byte[] subBytes(byte[] src, int begin, int count) {
byte[] bs = new byte[count];
System.arraycopy(src, begin, bs, 0, count);
return bs;
}
if (!StringUtil.isNullOrEmpty(po.getContent())){
byte[] contentB= new byte[0];
try {
contentB = po.getContent().getBytes("utf-8");
if (contentB.length>1024){
po.setContent(new String(StringUtil.subBytes(contentB,0,1024),"utf-8"));
}
} catch (UnsupportedEncodingException e) {
String result="content bytes length > 1024,convert error";
po.setContent(result);
log.error(result,e);
}
}

12.获取类的调用链路

    private Class<?> deduceMainApplicationClass() {
try {
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
if ("main".equals(stackTraceElement.getMethodName())) {
return Class.forName(stackTraceElement.getClassName());
}
}
}
catch (ClassNotFoundException ex) {
// Swallow and continue
}
return null;
}

最新文章

  1. Spring2.0-applicationContext.xml中使用el表达式给实体类属性赋值被当成字符串-遁地龙卷风
  2. iOS:崩溃统计工具Crashlytics的使用
  3. jcmd、jmc介绍
  4. 微信公众平台开放JS-SDK(微信内网页开发工具包)
  5. shell-bash学习04读取输入、分隔符、流程控制
  6. 应该具备的调试技能(java)
  7. 《利用python进行数据分析》读书笔记--第十一章 金融和经济数据应用(一)
  8. discuz 帖子模块用到的表及自动发帖函数
  9. 【转】kylin优化
  10. ASP.net Application及Session 的start end 方法总结
  11. 实战EntityFramework
  12. cocos2d-x-3.1 事件分发机制 (coco2d-x 学习笔记七)
  13. 关于iTunes随机播放和我所不知道的自己
  14. 编写Qt Designer自定义控件
  15. NModBus的使用
  16. delphi 按键测试
  17. VSCode插件开发全攻略(九)常用API总结
  18. 安装Oracle Database 11g 找不到文件“WFMLRSVCApp.ear” .
  19. 【题解】Luogu P2472 [SCOI2007]蜥蜴
  20. android margin--负的margin的使用

热门文章

  1. day01_2spring3
  2. 简单易用,用Powershell劫持Windows系统快捷键
  3. php设计模式之桥接模式实例代码
  4. K3/Cloud点击按钮打开第三方URL
  5. [AtCoder]Grand Contest 028
  6. flume--exec源
  7. SpringBoot整合WEB开发--(三)文件上传
  8. util之Stack
  9. 三维数据曲面图 season绘图 panda绘图
  10. anaconda+pytorch安装(无GPU版本)