title: How to include properties from external file to hibernate.cfg.xml using spring in a standalone app(jar)
body: <p>I need to be able to store database config properties in an external file that well be used by the application jar and include it  in form of jstl expressions. (like : ${password} etc.)?</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
  &lt;!DOCTYPE hibernate-configuration PUBLIC  
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;  
  
  &lt;hibernate-configuration&gt;  
  
  &lt;session-factory&gt;  
   &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;  
    &lt;property name="dialect"&gt;org.hibernate.dialect.DB2Dialect&lt;/property&gt;
    &lt;property name="connection.url"&gt;jdbc:db2://localhost:50001/svntools&lt;/property&gt; 
    &lt;property name="connection.username"&gt;root&lt;/property&gt; 
    &lt;property name="connection.password"&gt;root&lt;/property&gt; 
    &lt;property name="connection.driver_class"&gt;com.ibm.db2.jcc.DB2Driver&lt;/property&gt; 
  --&gt;
  
       &lt;property name="show_sql"&gt;true&lt;/property&gt;  
  
  
   &lt;mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.BrancheEntity"/&gt;  
   &lt;mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.RevisionEntity"/&gt;  
   &lt;mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.ProjectEntity"/&gt;  
   &lt;mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.StatistiqueEntity"/&gt;  
   &lt;mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.DomaineEntity"/&gt;  
  
  
   &lt;/session-factory&gt;  
  
  &lt;/hibernate-configuration&gt;  
  </code></pre>
  
  <p>SpringConfig.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  </code></pre>
  
  <p>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      <a href="http://www.springframework.org/schema/context" rel="nofollow noreferrer">http://www.springframework.org/schema/context</a>
      <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd" rel="nofollow noreferrer">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>"></p>
  
  <pre><code>&lt;bean id="projectDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.ProjectDAOImpl"&gt;
  &lt;/bean&gt;
  &lt;bean id="reportDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.ReportDAOImpl" /&gt;
  &lt;bean id="brancheDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.BrancheDAOImpl" /&gt;
  
  &lt;bean id="domaineDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.DomaineDAOImpl" /&gt;
  
  &lt;bean id="svnKitDa"
      class="fr.gouv.education.sirhen.svnreporting.domaine.DA.impl.SVNKitDAImpl" /&gt;
  &lt;bean id="RevisionServicesBean"
      class="fr.gouv.education.sirhen.svnreporting.domaine.impl.RevisionsServicesImpl"&gt;
      &lt;property name="svnKitDa" ref="svnKitDa" /&gt;
      &lt;property name="brancheDAO" ref="brancheDAO" /&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="parser" class="fr.gouv.education.sirhen.svnreporting.transvers.utils.ParserImpl" /&gt;
  
  &lt;bean id="reportServices"
      class="fr.gouv.education.sirhen.svnreporting.service.impl.ReportServicesImpl"&gt;
      &lt;property name="reportDAO" ref="reportDAO" /&gt;
      &lt;property name="brancheDAO" ref="brancheDAO" /&gt;
      &lt;property name="projectDAO" ref="projectDAO" /&gt;
      &lt;property name="parser" ref="parser" /&gt;
  &lt;/bean&gt;
  &lt;bean id="projectServices"
      class="fr.gouv.education.sirhen.svnreporting.service.impl.ProjectServicesImpl"&gt;
      &lt;property name="projectDAO" ref="projectDAO" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="domaineServices"
      class="fr.gouv.education.sirhen.svnreporting.service.impl.DomaineServicesImpl"&gt;
      &lt;property name="domaineDAO" ref="domaineDAO" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="generator"
      class="fr.gouv.education.sirhen.svnreporting.domaine.generatorDocServices.impl.GeneratorDocServiceImpl" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>The class that use the session:</p>
  
  <pre><code>package fr.gouv.education.sirhen.svnreporting.persistance.impl;
  
  
  import java.io.File;
  import java.util.LinkedList;
  import java.util.List;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.hibernate.cfg.AnnotationConfiguration;
  import org.hibernate.cfg.Configuration;
  
  import fr.gouv.education.sirhen.svnreporting.persistance.ProjectDAO;
  import fr.gouv.education.sirhen.svnreporting.persistance.eo.ProjectEntity;
  
  public class ProjectDAOImpl implements ProjectDAO {
  
      private static final String Location_Hibernate = 
              "resources/hibernate.cfg.xml";
      private SessionFactory sessionFactory;
  
       public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
  
      public void addProject(ProjectEntity project) {
          File hibernatePropsFile = new File(Location_Hibernate);
           Session session=new Configuration().configure(hibernatePropsFile).buildSessionFactory().openSession();  
             Transaction t=session.beginTransaction();  
             session.saveOrUpdate(project);
             t.commit();
             session.close();  
      }
  
      public List&lt;ProjectEntity&gt; getProjects() {
          File hibernatePropsFile = new File(Location_Hibernate);
           Session session=new Configuration().configure(hibernatePropsFile).buildSessionFactory().openSession();  
             Transaction t=session.beginTransaction();  
  
            List&lt;ProjectEntity&gt; projects= session.createCriteria(ProjectEntity.class).list();
             t.commit();  
             session.close();  
             return projects;
      }
  
      public List&lt;String&gt; getProjectsNames() {
          File hibernatePropsFile = new File(Location_Hibernate);
           Session session=new Configuration().configure(hibernatePropsFile).buildSessionFactory().openSession();  
             Transaction t=session.beginTransaction();  
  
            List&lt;ProjectEntity&gt; projects= session.createCriteria(ProjectEntity.class).list();
             t.commit();  
             session.close();  
             List&lt;String&gt; ProjectsNames=new LinkedList&lt;String&gt;();
             for( ProjectEntity projet : projects)
             {
                 ProjectsNames.add(projet.getName());   
             }
             return ProjectsNames;
      }
  
  
  
  }
  </code></pre>