java不常用的一些小东西

来源:互联网 发布:windows键的用途 编辑:程序博客网 时间:2024/05/21 10:59

一:获取一个类所在的jar包的绝对路径

       String jarPath = XXXXX.class.getProtectionDomain().getCodeSource().getLocation().getFile();        try {            jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");        } catch (Exception e) {            e.printStackTrace();        }

二:javaWeb项目获取ApplicationContext对象

private static WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();

三:获取IP地址

            InetAddress address = InetAddress.getLocalHost();            ipAddress = address.getHostAddress().toString();

四:在基类中获取子类传入的泛型类型


import java.lang.reflect.Type;import java.lang.reflect.ParameterizedType;public class BaseDao<T> {public BaseDao(){Type type=getClass().getGenericSuperclass();Type[]param=((ParameterizedType)type).getActualTypeArguments();Class entiyClass=(Class) param[0];}}

五:





0 0
原创粉丝点击