首页 文章

如何在独立应用程序(jar)中使用spring将外部文件中的属性包含到hibernate.cfg.xml

提问于
浏览
2

我需要能够将数据库配置属性存储在应用程序jar使用的外部文件中,并以jstl表达式的形式包含它 . (比如:$ {密码}等)?

<?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文件

<?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" />

使用会话的类:

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 回答

  • 1

    另一种方法是您可以直接使用 hibernate.properties 文件而不是 hibernate.cfg.xml .

    但是如果你想使用另一个文件然后 hibernate.properties 文件,请参考下面给出的链接:

    How to read database configuration parameter using properties file in hibernate

    尽管如此,如果要分别读取属性文件,则可以使用普通的java代码读取类路径或相对文件路径中的属性文件,并使用 ConfigurableEnvironment of spring在环境中设置这些属性 .

    已编辑答案

    如果要读取应用程序(jar)之外的属性文件,则可以从相对文件路径以编程方式读取属性文件 . 我之前提供了一个答案,读取属性文件的情况相同,您可以从那里按照我的编辑答案 .

    Spring Boot embedded Tomcat not loading external properties file in ApplicationListener

    现在,您可以使用系统属性或环境属性来存储先前从相对文件路径加载的属性,然后它将在应用程序中的任何位置可用 .

    @Autowired
    private ConfigurableEnvironment  myEnv;
    

    要么

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

相关问题