Tomcat 不创建 "new Property()" 实例

Tomcat doesn't create the "new Property()" instance

Whosebug,请帮助我。我有一个小型 Web 应用程序 (Servlet + jsp)。单元测试正确通过,但部署后我的工厂无法创建我的 DAO 实例,因为在操作 "Property property = new Property();" property = null 之后。为什么?(

public class DAOFactory <T>{

    private String daoType;
    private String propertyFilePath;
    private Properties property;
    private FileInputStream fis;
    private static Logger LOGGER;
    private String propertyKey;

    public DAOFactory(String propertyFilePath,String propertyKey) {
        this(propertyKey);
        this.propertyFilePath = propertyFilePath;
    }


    public DAOFactory(String propertyKey) {
        propertyFilePath = "src/main/resources/dao_factory.properties";
        LOGGER = LoggerFactory.getLogger(DAOFactory.class);
        this.propertyKey = propertyKey;
        try {
            property = new Properties();
            fis = new FileInputStream(propertyFilePath);
            property.load(fis);
        } catch (FileNotFoundException ex) {
            LOGGER.error("Property file " + propertyFilePath + " doesn't exist", ex);
        } catch (IOException ex) {
            LOGGER.error("Unable to download Property file: " + propertyFilePath, ex);
        }
        System.err.println("fis: " + fis);
        System.err.println("propertyKey: " + propertyKey);
        System.err.println("property: " + property);
        daoType = property.getProperty(propertyKey);
        System.err.println("daoType: " + daoType);
    }



    public T getInstance () throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
        Class c = Class.forName(daoType); 
        Method method = c.getDeclaredMethod("getInstance");
        return (T) method.invoke(null, null);
    }

}

当我尝试使用我的 DAOFactory, DAOFactory<BookDAO> daoFactory= new DAOFactory(""BookDAO"); // or even new DAOFactory("src/main/resources/dao_factory.properties","BookDAO");

我有

测试中

fis: java.io.FileInputStream@5025a98f
propertyKey: BookDAO
property: {BookDAO=com.softserve.siniaieva.bibliophile.dao.impl.BookDAOImitation, ReaderDAO=com.softserve.siniaieva.bibliophile.dao.impl.ReaderDAOImitation}
daoType: com.softserve.siniaieva.bibliophile.dao.impl.BookDAOImitation

WHEN TOMCAT CREATES DAOFactory
fis: null
propertyKey: BookDAO
property: null
daoType: null  

我应该在 web.xml 中为 Tomcat 添加 smth 以使其看到 FileInputStream 吗?

问题可能与此有关

propertyFilePath = "src/main/resources/dao_factory.properties"; 

打包后src目录将不可用,而是分别移动到bin目录下。试一试下面的内容

DAOFactory.class.getResourceAsStream("dao_factory.properties")

您可以阅读更多相关信息 here