简体   繁体   English

如何从外部项目文件夹访问hibernate.cfg.xml,config.properties和log4j.properties

[英]How to Access hibernate.cfg.xml, config.properties, and log4j.properties from outside project folder

I want to export my java project (using eclipse) into executable jar, but I want hibernate.cfg.xml, config.properties, and log4j.properties editable for future, so how to make hibernate access that file from outside project folder or any other way to make that file editable for future, 我想将我的Java项目(使用eclipse)导出到可执行jar中,但是我希望将来可以对hibernate.cfg.xml,config.properties和log4j.properties进行编辑,因此如何使hibernate从项目文件夹或任何外部文件访问该文件使该文件可编辑以备将来使用的另一种方法,

I have try this code for acces hibernate.cfg.xml from outside project folder 我已经尝试从外部项目文件夹访问acces hibernate.cfg.xml的代码

SessionFactory sessionFactory = new Configuration().configure("mon/hibernate.cfg.xml").buildSessionFactory();

but i got this error 但是我得到这个错误

mon/hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError

and still have no idea about config.properties and log4j.properties, any help will be pleasure :) 仍然对config.properties和log4j.properties仍然一无所知,任何帮助都将是很高兴的:)

there is my solution to your problem: 我有解决您问题的方法:

config.properties config.properties

You define your configuration file trough parameter -DconfigurationFile set to your JVM. 您可以将配置文件通过参数-DconfigurationFile设置为JVM。 Then try to find confiFile in your classpath (inside jar) if is not found then filesystem will be searched. 然后尝试在您的classpath (在jar中)中找到confiFile ,如果找不到,则将搜索文件系统。 Well, as last the properties will be override with JVM parameters. 好了,最后,这些属性将被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.properties log4j.properties

The solution is using of the following JVM parameter: 解决方案使用以下JVM参数:

-Dlog4j.configuration={path to file}

If the file is NOT in the classpath (in WEB-INF/classes in case of Tomcat) but somewhere on you disk, use file:, like 如果该文件不在类路径中(对于Tomcat,则为WEB-INF / classes),但不在您的磁盘上,请使用file :,例如

-Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties

hibernate.cfg.xml hibernate.cfg.xml

I have no idea how to do this. 我不知道该怎么做。 Anyway, it hard to configure persistance after release because the configuration is hard bind to implementation. 无论如何,发布后配置持久性很困难,因为配置很难绑定到实现上。 I think it is OK to keep it inside classpath. 我认为可以将其保留在classpath中。

You can instruct hibernate to load config file from file system, 您可以指示休眠从文件系统中加载配置文件,

There are lots of overloaded configure() methods are available, see the link for the documentaion 有很多重载的configure()方法可用,请参见文档链接。

and below is the way you can do: 下面是您可以执行的方法:

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

http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/cfg/Configuration.html#configure(java.io.File) http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/cfg/Configuration.html#configure(java.io.File)

and for log4j, you can give -D argument with log4j config file 对于log4j,您可以在log4j配置文件中提供-D参数

like -Dlog4j.configuration={path to .properties or .xml } -Dlog4j.configuration={path to .properties or .xml }

similar question for log4j externalization : How to initialize log4j properly? log4j外部化的类似问题: 如何正确初始化log4j?

It will be worth reading that as well. 值得一读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM