properties文件的读取

nacos版本:1.1.4

import static com.alibaba.nacos.common.util.ResourceUtils.getResourceAsStream;

nacos版本:1.3.3

import static com.alibaba.nacos.common.utils.ResourceUtils.getResourceAsStream;
//不适用于.jar包中文件读取
//InputStream in = new BufferedInputStream(new FileInputStream(Property.class.getClassLoader().getResource("simplelog.properties").getPath()));
//适用于源码文件和.jar包文件读取
InputStream in = getResourceAsStream("simplelog.properties");

Commons Logging会使用Log4j和它在上下文类路径中找到的log4j.properties文件。

//查看commons Logging的源码,发现在simpleLog.java的类里有这么读取properties文件的。
InputStream in = getResourceAsStream("simplelog.properties");
   /**
    * Return the thread context class loader if available.
    * Otherwise return null.
    *
    * The thread context class loader is available for JDK 1.2
    * or later, if certain security conditions are met.
    *
    * @exception LogConfigurationException if a suitable class loader
    * cannot be identified.
    */
    private static ClassLoader getContextClassLoader()
    {
       ClassLoader classLoader = null;

       if (classLoader == null) {
           try {
               // Are we running on a JDK 1.2 or later system?
               Method method = Thread.class.getMethod("getContextClassLoader", null);

               // Get the thread context class loader (if there is one)
               try {
                   classLoader = (ClassLoader)method.invoke(Thread.currentThread(), null);
               } catch (IllegalAccessException e) {
                   ;  // ignore
               } catch (InvocationTargetException e) {
                   /**
                    * InvocationTargetException is thrown by 'invoke' when
                    * the method being invoked (getContextClassLoader) throws
                    * an exception.
                    *
                    * getContextClassLoader() throws SecurityException when
                    * the context class loader isn't an ancestor of the
                    * calling class's class loader, or if security
                    * permissions are restricted.
                    *
                    * In the first case (not related), we want to ignore and
                    * keep going.  We cannot help but also ignore the second
                    * with the logic below, but other calls elsewhere (to
                    * obtain a class loader) will trigger this exception where
                    * we can make a distinction.
                    */
                   if (e.getTargetException() instanceof SecurityException) {
                       ;  // ignore
                   } else {
                       // Capture 'e.getTargetException()' exception for details
                       // alternate: log 'e.getTargetException()', and pass back 'e'.
                       throw new LogConfigurationException
                           ("Unexpected InvocationTargetException", e.getTargetException());
                   }
               }
           } catch (NoSuchMethodException e) {
               // Assume we are running on JDK 1.1
               ;  // ignore
           }
       }

       if (classLoader == null) {
           classLoader = SimpleLog.class.getClassLoader();
       }

       // Return the selected class loader
       return classLoader;
    }

    private static InputStream getResourceAsStream(final String name)
    {
       return (InputStream)AccessController.doPrivileged(
           new PrivilegedAction() {
               public Object run() {
                   ClassLoader threadCL = getContextClassLoader();

                   if (threadCL != null) {
                       return threadCL.getResourceAsStream(name);
                   } else {
                       return ClassLoader.getSystemResourceAsStream(name);
                   }
               }
           });
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值