2017-08-02 33 views
1

我需要能够将数据库配置属性存储在外部文件中,该文件可以被应用程序jar使用并包含它以jstl表达式的形式。 (如:$ {密码}等)?如何在独立应用程序(jar)中使用spring将外部文件的属性包含到hibernate.cfg.xml中

<?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/contexthttp://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; 
    } 



} 
+0

嗨@Yasser马利克,你问是不是足够清晰,但仍然按我的理解,我可以提供你一个答案 –

+0

正在开发一个独立的应用程序(可执行的JAR文件),我想能够使hibernate.cfg中定义的db连接信息使用属性文件从jar外部参数化 –

+0

我将更新我的答案相应地,您可以按照 –

回答

1

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

但是,如果你想使用其他文件,然后hibernate.properties文件,那么请参考链接如下:

How to read database configuration parameter using properties file in hibernate

不过,如果你想读属性文件分开,然后你可以用普通的Java代码阅读从类路径或相对文件路径读取属性文件,并使用spring的ConfigurableEnvironment设置环境中的属性。

编辑答案

如果你想读属性文件应用程序(JAR),那么你可以阅读的场所相对文件路径编程文件外。 我刚才提供了一个答案,这与阅读属性文件的情况相同,您可以按照我编辑的答案。

Spring Boot embedded Tomcat not loading external properties file in ApplicationListener

现在你可以使用系统属性或环境属性来存储从相对文件路径更早加载的属性,然后它会提供的任何地方的应用程序。

@Autowired 
private ConfigurableEnvironment myEnv; 

System.setProperty ("propertyName", "propertyValue"); 
相关问题