zoukankan      html  css  js  c++  java
  • 项目外部 property文件使用方法

    web.xml

    <env-entry>
    <env-entry-name>TestConfigs</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>/app/conf/server/api.properties</env-entry-value>
    </env-entry>

    java代码:

    public class ConfigUtils {

    private final static ConfigUtils INSTANCE = new ConfigUtils();

    public static ConfigUtils getInstance() {
    return INSTANCE;
    }
    private static final String MAX_BALANCE = "maxbalance";//"100";


    public String getRedenvelopeUnvaliMaxBalance() {
    return getProperties().getProperty(MAX_BALANCE);
    }

    public static Properties props = null;

    public static Properties getProperties() {

    if (props == null) {
    props = new Properties();
    InputStream fileStream = null;
    Context env = null;
    String path = null;
    try {
    // Get a handle to the JNDI environment naming context
    env = (Context) new InitialContext().lookup("java:comp/env");
    path = (String) env.lookup("TestConfigs");

    File file = new File(path);
    file = file.getAbsoluteFile();
    fileStream = new FileInputStream(file);

    if (logger.isDebugEnabled()) {
    logger.debug("Opened config file <" + path
    + "> successfully.");
    }
    } catch (FileNotFoundException e1) {
    logger.error(path
    + " is not found in local environment. Trying the classpath....");
    // do nothing yet
    } catch (NamingException e) {
    logger.error(path + " pathname is erroneous. ", e);
    }

    try {
    if (fileStream == null) {
    props.load(ConfigUtil.class.getClassLoader()
    .getResourceAsStream(path));
    } else {
    // HOSTSFILE was found
    props.load(fileStream);
    }
    } catch (IOException e) {
    logger.fatal("Cannot find and load " + path
    + "!! Make sure it is available!", e);
    }
    }
    return props;
    }

    }

    Mark:涉及到的知识,jndi、java.until.Properties、 ConfigUtil.class.getClassLoader()
    .getResourceAsStream 用法

  • 相关阅读:
    Docker入门
    KMP算法
    spring boot整合Thymeleaf
    thymeleaf公共页面元素抽取
    入门oj 6492: 小B的询问
    入门oj 6451: The XOR Largest Pair之二
    入门oj 5499: 讲话模式
    把Windows CA根证书安装到iPhone
    笔记本电脑键盘状态助手KeyboardState
    开启Windows7多用户远程桌面
  • 原文地址:https://www.cnblogs.com/landauni/p/8489262.html
Copyright © 2011-2022 走看看