Java 如何从项目文件夹外部访问hibernate.cfg.xml、config.properties和log4j.properties
我想将我的java项目(使用eclipse)导出到可执行jar中, 但我希望hibernate.cfg.xml、config.properties和log4j.properties在将来可以编辑, 那么,如何使hibernate从项目文件夹外部访问该文件,或者以任何其他方式使该文件可编辑以备将来使用 我已经尝试了从项目文件夹外部访问hibernate.cfg.xml的代码Java 如何从项目文件夹外部访问hibernate.cfg.xml、config.properties和log4j.properties,java,xml,eclipse,hibernate,Java,Xml,Eclipse,Hibernate,我想将我的java项目(使用eclipse)导出到可执行jar中, 但我希望hibernate.cfg.xml、config.properties和log4j.properties在将来可以编辑, 那么,如何使hibernate从项目文件夹外部访问该文件,或者以任何其他方式使该文件可编辑以备将来使用 我已经尝试了从项目文件夹外部访问hibernate.cfg.xml的代码 SessionFactory sessionFactory = new Configuration().configure(
SessionFactory sessionFactory = new Configuration().configure("mon/hibernate.cfg.xml").buildSessionFactory();
但我犯了这个错误
mon/hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
仍然不知道config.properties和log4j.properties,
任何帮助都将是我的荣幸:)我有办法解决您的问题: 配置属性 您可以通过将参数
-DconfigurationFile
设置为JVM来定义配置文件。然后尝试在类路径(jar内部)中查找confiFile
,如果未找到,则将搜索文件系统。最后,属性将被JVM参数覆盖
Properties prop = new Properties();
String configFile = System.getProperty("configurationFile",defaultConfigurationFile);
try {
InputStream classPathIo = getClass().getClassLoader().getResourceAsStream(configFile);
if(classPathIo != null) {
prop.load(classPathIo);
} else {
prop.load(new FileReader(configFile));
} catch (FileNotFoundException e) {
log.warn("The config file {} cannot be found. It can be setup by -DconfigurationFile parameter.",configFile);
} catch (IOException e) {
log.warn("The config file {} is not readable.",configFile);
} finally {
log.info("Configuration loaded! {} values found from configFile {}.",prop.entrySet().size(),configFile);
prop.putAll(System.getProperties());
}
log4j.属性
解决方案使用以下JVM参数:
-Dlog4j.configuration={path to file}
如果文件不在类路径中(对于Tomcat,在WEB-INF/classes中),而是在磁盘上的某个地方,请使用file:,如
-Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties
hibernate.cfg.xml
我不知道怎么做。无论如何,在发布后很难配置持久性,因为配置很难绑定到实现。我认为可以将其保存在类路径中。您可以指示hibernate从文件系统加载配置文件
有很多重载的configure()
方法可用,请参阅文档链接
下面是您可以使用的方法:
File conf = new File(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml");
Configuration configuration = new Configuration().configure(conf.getAbsoluteFile());
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
对于log4j,可以使用log4j配置文件给出-D参数
比如-Dlog4j.configuration={path to.properties或.xml}
log4j外部化的类似问题:
这也值得一读。很抱歉,我以前从未使用过JVM参数。。。我是否为此代码创建了一个新文件-Dlog4j.configuration=file:/where/on/disk/log4j.properties
或我必须添加此代码的where文件?我只是将一个Java项目设置为非Web项目或其他。。。将来,这个java将在命令提示符(可执行jar)中运行,使用这个命令java-jar
任何想法??然后只需添加java-Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties-DconfigurationFile=/somewhere/config.properties-jar
对我仍然不起作用,我在Loader.getResources中遇到了这个错误log4j:WARN捕获异常。这可能是无害的。java.lang.IllegalArgumentException:name
对不起,我还是不明白。。。其中,我必须添加这个代码的文件file conf=new文件(ABS_PATH_to_CONFIG+file.separator+“hibernate.cfg.xml”);配置=新配置().configure(conf.getAbsoluteFile())
进入我的控制器或DBUtil???@splatter\u fadli:编辑ans,将其放在构建会话工厂的位置