"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
吗?
答案 0 :(得分:0)
问题可能与此相关
propertyFilePath = "src/main/resources/dao_factory.properties";
打包后src
目录不可用,而是将其分别移动到bin
目录。给出类似下面的内容
DAOFactory.class.getResourceAsStream("dao_factory.properties")
您可以阅读有关此here的更多信息。