How to include properties from external file to hibernate.cfg.xml using spring in a standalone app(jar)

Viewed 3579

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.)?

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

<hibernate-configuration>  

<session-factory>  
 <property name="hbm2ddl.auto">update</property>  
  <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
  <property name="connection.url">jdbc:db2://localhost:50001/svntools</property> 
  <property name="connection.username">root</property> 
  <property name="connection.password">root</property> 
  <property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property> 
-->

     <property name="show_sql">true</property>  


 <mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.BrancheEntity"/>  
 <mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.RevisionEntity"/>  
 <mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.ProjectEntity"/>  
 <mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.StatistiqueEntity"/>  
 <mapping class="fr.gouv.education.sirhen.svnreporting.persistance.eo.DomaineEntity"/>  


 </session-factory>  

</hibernate-configuration>  

SpringConfig.xml file

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="projectDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.ProjectDAOImpl">
</bean>
<bean id="reportDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.ReportDAOImpl" />
<bean id="brancheDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.BrancheDAOImpl" />

<bean id="domaineDAO" class="fr.gouv.education.sirhen.svnreporting.persistance.impl.DomaineDAOImpl" />

<bean id="svnKitDa"
    class="fr.gouv.education.sirhen.svnreporting.domaine.DA.impl.SVNKitDAImpl" />
<bean id="RevisionServicesBean"
    class="fr.gouv.education.sirhen.svnreporting.domaine.impl.RevisionsServicesImpl">
    <property name="svnKitDa" ref="svnKitDa" />
    <property name="brancheDAO" ref="brancheDAO" />
</bean>


<bean id="parser" class="fr.gouv.education.sirhen.svnreporting.transvers.utils.ParserImpl" />

<bean id="reportServices"
    class="fr.gouv.education.sirhen.svnreporting.service.impl.ReportServicesImpl">
    <property name="reportDAO" ref="reportDAO" />
    <property name="brancheDAO" ref="brancheDAO" />
    <property name="projectDAO" ref="projectDAO" />
    <property name="parser" ref="parser" />
</bean>
<bean id="projectServices"
    class="fr.gouv.education.sirhen.svnreporting.service.impl.ProjectServicesImpl">
    <property name="projectDAO" ref="projectDAO" />
</bean>

<bean id="domaineServices"
    class="fr.gouv.education.sirhen.svnreporting.service.impl.DomaineServicesImpl">
    <property name="domaineDAO" ref="domaineDAO" />
</bean>

<bean id="generator"
    class="fr.gouv.education.sirhen.svnreporting.domaine.generatorDocServices.impl.GeneratorDocServiceImpl" />

The class that use the session:

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<ProjectEntity> getProjects() {
        File hibernatePropsFile = new File(Location_Hibernate);
         Session session=new Configuration().configure(hibernatePropsFile).buildSessionFactory().openSession();  
           Transaction t=session.beginTransaction();  

          List<ProjectEntity> projects= session.createCriteria(ProjectEntity.class).list();
           t.commit();  
           session.close();  
           return projects;
    }

    public List<String> getProjectsNames() {
        File hibernatePropsFile = new File(Location_Hibernate);
         Session session=new Configuration().configure(hibernatePropsFile).buildSessionFactory().openSession();  
           Transaction t=session.beginTransaction();  

          List<ProjectEntity> projects= session.createCriteria(ProjectEntity.class).list();
           t.commit();  
           session.close();  
           List<String> ProjectsNames=new LinkedList<String>();
           for( ProjectEntity projet : projects)
           {
               ProjectsNames.add(projet.getName());   
           }
           return ProjectsNames;
    }



}
1 Answers
Related