无法在 Tomcat 应用程序中加载 .properties

发布于 2024-10-05 19:02:12 字数 2912 浏览 2 评论 0原文


我需要编写一个连接到数据库并从 .properties 文件中读取连接属性的 Web 应用程序。
我使用 Maven,我想为我的应用程序编写一些单元测试。
为了测试与数据库一起使用的类,我将 .properties 文件放入 src/main/resources 和 test/main/resources 并编写以下代码来读取属性:

package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;


/**
 *
 * @author proger
 */
public class ProgrammSettings {

    private static Logger logger = Logger.getRootLogger();
    private static String propertiesPath = "program.settings.properties";

    private static Properties props;

    public static String getProperty(String key) {
        if (props == null)
            loadProperties();

       return props.getProperty(key);

    }

    public static void setResourcesFilePath(String path) {
        propertiesPath = path;
    }

    private static void loadProperties() {
        InputStream propOut = null;
        try {
            //URL url =  ClassLoader.getSystemResource(propertiesPath);
            propOut = ClassLoader.getSystemClassLoader().getResourceAsStream(propertiesPath); //new FileInputStream(url.getFile());
            props = new Properties();
            props.load(propOut);
        } catch (Exception ex) {
            //Logger.getLogger(ProgrammSettings.class.getName()).log(Level.SEVERE, null, ex);
            String errorMessage = "Error during properties file reading. Path to file: '" +
                           propertiesPath + "'";
            logger.error(errorMessage, ex);
            throw new RuntimeException(errorMessage, ex);
        } finally {
            try {
                if (propOut != null)
                    propOut.close();
            } catch (IOException ex) {
                logger.error("Error during closing stream from properties file", ex);
            }
        }
    }
}

我希望相同的代码适用于测试和应用程序类,所以我从 classPath 读取属性文件。 我所有的测试都通过了,我的 .properties 文件被 Maven 打包到 war 中。但是,当我从 Netbeans 运行我的应用程序时,我收到以下消息:

java.lang.NullPointerException
    java.util.Properties$LineReader.readLine(Properties.java:435)
    java.util.Properties.load0(Properties.java:354)
    java.util.Properties.load(Properties.java:342)
    util.ProgrammSettings.loadProperties(ProgrammSettings.java:49)
    util.ProgrammSettings.getProperty(ProgrammSettings.java:33)
    dataacess.AbstractDataMapper.loadDriver(AbstractDataMapper.java:58)
    dataacess.AbstractDataMapper.<init>(AbstractDataMapper.java:41)
    dataacess.UserMapper.<init>(UserMapper.java:25)
    servlets.UserRegistration.processRequest(UserRegistration.java:86)
    servlets.UserRegistration.doPost(UserRegistration.java:132)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

我该如何修复它?先感谢您

I need to write a web-aplication that connects to database and reads connections properties from .properties file.
I use Maven and I want to write some unit test for my app.
To test classes that works with database I put my .properties file to src/main/resources and to test/main/resources and wrote the following code for reading properties:

package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;


/**
 *
 * @author proger
 */
public class ProgrammSettings {

    private static Logger logger = Logger.getRootLogger();
    private static String propertiesPath = "program.settings.properties";

    private static Properties props;

    public static String getProperty(String key) {
        if (props == null)
            loadProperties();

       return props.getProperty(key);

    }

    public static void setResourcesFilePath(String path) {
        propertiesPath = path;
    }

    private static void loadProperties() {
        InputStream propOut = null;
        try {
            //URL url =  ClassLoader.getSystemResource(propertiesPath);
            propOut = ClassLoader.getSystemClassLoader().getResourceAsStream(propertiesPath); //new FileInputStream(url.getFile());
            props = new Properties();
            props.load(propOut);
        } catch (Exception ex) {
            //Logger.getLogger(ProgrammSettings.class.getName()).log(Level.SEVERE, null, ex);
            String errorMessage = "Error during properties file reading. Path to file: '" +
                           propertiesPath + "'";
            logger.error(errorMessage, ex);
            throw new RuntimeException(errorMessage, ex);
        } finally {
            try {
                if (propOut != null)
                    propOut.close();
            } catch (IOException ex) {
                logger.error("Error during closing stream from properties file", ex);
            }
        }
    }
}

I wanted the same code worked both for tests and app classes, so I read properties file from classPath.
All my tests pass and my .properties file is packed into war by Maven. But when I run my App from Netbeans this I receive:

java.lang.NullPointerException
    java.util.Properties$LineReader.readLine(Properties.java:435)
    java.util.Properties.load0(Properties.java:354)
    java.util.Properties.load(Properties.java:342)
    util.ProgrammSettings.loadProperties(ProgrammSettings.java:49)
    util.ProgrammSettings.getProperty(ProgrammSettings.java:33)
    dataacess.AbstractDataMapper.loadDriver(AbstractDataMapper.java:58)
    dataacess.AbstractDataMapper.<init>(AbstractDataMapper.java:41)
    dataacess.UserMapper.<init>(UserMapper.java:25)
    servlets.UserRegistration.processRequest(UserRegistration.java:86)
    servlets.UserRegistration.doPost(UserRegistration.java:132)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

How can I fix it? Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2024-10-12 19:02:12

program.settings.properties 放在 webapps/appname/WEB-INF/classes

,您可以使用更简单的行 ProgrammSettings.class.getResourceAsStream(..)< /code> (即不是系统类加载器,而是当前类加载器)

Place program.settings.properties in webapps/appname/WEB-INF/classes

And you can use a simpler line ProgrammSettings.class.getResourceAsStream(..) (i.e. not the system classloader, but the current classloader)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文