我有一个通用 jar,用于使用数据源 XML 配置“db2.xml”创建数据库连接池,它位于此 JAR 的同一路径下,例如:
Project/
-- lib
-- db2.xml
-- common.jar
按照读取 db2.xml 的代码:
private BeanFactory() {
try {
beanFactory = new DefaultListableBeanFactory();
xmlReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)
beanFactory);
resource = new ClassPathResource("db2.xml");
xmlReader.loadBeanDefinitions(resource);
} catch (Exception e) {
e.printStackTrace();
}
}
总是发生错误:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException
parsing XML document from class path resource [db2.xml]; nested exception is
java.io.FileNotFoundException: class path resource [db2.xml] cannot be opened
because it does not exist
所以肯定是找不到'db2.xml'造成的。是否设置了配置文件resource = new ClassPathResource("/db2.xml")
或resource = new ClassPathResource("lib/db2.xml")
; resource = new ClassPathResource("../lib/db2.xml")
这一切都不起作用。如何为此设置相对路径。
resource = new ClassPathResource(CONFIGURATION_PATH);
这是一个 Java 项目。当我将 db2.xml 放入公共 jar 时,我工作。