【问题标题】:How to load jdbc. properties file dynamically using spring?如何加载jdbc。使用spring动态属性文件?
【发布时间】:2023-03-31 03:50:02
【问题描述】:

我的产品支持两个以上的数据库。 我在我的产品中使用 common.properties 文件

数据库配置描述是

Product.database=XXXX (oracle,postgres or mysql)

如何动态加载XXXX(数据库)jdbc.properties文件?

这是可能的。帮帮我

【问题讨论】:

    标签: hibernate spring spring-mvc spring-jdbc


    【解决方案1】:

    也许你可以使用Properties 类,它提供了一个load 方法。

    如果你使用 Spring Framework,也许你想看看PropertyPlaceholderConfigurer

    【讨论】:

    • 只是为了理解:您是尝试直接加载属性,还是希望 Spring 框架为您引导它们?
    【解决方案2】:

    主要用于我的应用程序,基于 Spring + JPA + Hibernate。我正在使用文件属性 让我们Spring读取属性,你也可以在spring xml配置中定义多个数据源。

    datasource.properties #休眠属性 hibernate.hbm2ddl.auto=create-drop hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=true dataSource.driverClassName=org.postgresql.Driver dataSource.url=jdbc:postgresql://localhost:5432/myproject dataSource.username=postgres dataSource.password=123456

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-autowire="byName">
    
    <!--  import another XML configuration -->
    <import resource="datasourceContext.xml"/>
    
    <context:annotation-config/>
    <context:component-scan base-package="com.mycompany"/>
    <tx:annotation-driven  />
    

    datasourceContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-autowire="byName">
    
    <context:property-placeholder location="classpath:datasource.properties"/>
    
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${dataSource.driverClassName}" p:url="${dataSource.url}"
        p:username="${dataSource.username}" p:password="${dataSource.password}" />
    
    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory" p:dataSource-ref="dataSource">
        <property name="persistenceUnitName" value="myPU" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                    p:database="POSTGRESQL" 
                    p:showSql="true" 
                    p:generateDdl="true" />
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
    

    这里是 persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com   
                 /xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
    </persistence>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 2012-03-23
      • 2019-06-17
      • 2012-07-23
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      相关资源
      最近更新 更多