Tomcat没有创建"新的Property()"例

时间:2016-01-31 17:17:54

标签: java tomcat fileinputstream

stackoverflow,请帮帮我。我有一个小型Web应用程序(Servlet + jsp)。单元测试正确传递,但部署后我的工厂无法创建我的DAO实例,因为在行动"Property property = new Property();" property = nul之后。为什么呢?(

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");

我有

IN TESTS

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吗?

1 个答案:

答案 0 :(得分:0)

问题可能与此相关

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

打包后src目录不可用,而是将其分别移动到bin目录。给出类似下面的内容

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

您可以阅读有关此here的更多信息。