3
votes

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
Hi @Yasser Malek, your ask is not clear enough but still as per my understanding i can provide you an answerIrfan Bhindawala
am developing a standalone app (executable jar) and I want to be able to make db connection info defined in hibernate.cfg parametred from outside the jar using a propertis fileYasser Malek
I will update my answer accordingly you can follow thatIrfan Bhindawala
I have edited my question so that you can relateYasser Malek
you can see that i have updated my answer for use the property file as per your requirement. You can use it in your dao while you using session factory.Irfan Bhindawala

1 Answers

1
votes

An alternate approach is you can directly use hibernate.properties file instead of hibernate.cfg.xml.

But if you want to use another file then hibernate.properties file then please refer link given below:

How to read database configuration parameter using properties file in hibernate

Still, if you want to read properties file separate then you can read with normal java code to read properties file from class path or relative file path and set those properties on environment using ConfigurableEnvironment of spring.

Edited Answer

If you want to read properties file outside of your application (jar) then you can read properties file programmatically from relative file path. I have provided one answer earlier and that was the same situation for read properties file, You can follow my Edited answer from there.

Spring Boot embedded Tomcat not loading external properties file in ApplicationListener

Now You can use System properties or Environment properties to store properties loaded earlier from relative file path and then it will available any where in the application.

@Autowired
private ConfigurableEnvironment  myEnv;

or

System.setProperty ("propertyName", "propertyValue");