美文网首页
spring加载外部属性文件进行数据库连接

spring加载外部属性文件进行数据库连接

作者: A_一只小菜鸟 | 来源:发表于2020-10-29 20:20 被阅读0次

意义

进行软编码,对spring配置中需要经常变动的属性提取到外部属性文件中,减少对配置文件的改动,方便环境部署。如数据库连接的用户名、密码等。

项目目录

fd4e58c609d95e18cafd3d2146aa545.png

jdbc.properties的内容:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/bnsg?characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&rewriteBatchedStatements=true
jdbc.password=Smartsecuri@6300
#jdbc.password=configAdmin
jdbc.username=root

内容是连接数据库信息。

在spring-platform-mybatis.xml中的配置

 <!-- 加载数据库属性配置文件 -->
    <context:property-placeholder
        location="classpath:config/db/jdbc/jdbc.properties" />

    <!-- 配置数据库C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxPoolSize" value="50"></property>
        <property name="minPoolSize" value="1"></property>
        <property name="initialPoolSize" value="1"></property>
        <property name="maxIdleTime" value="20"></property>
    </bean>

加载多个properties时:

 <!-- 将多个配置文件读取到容器中,交给Spring管理 -->  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
           <list>  
              <!-- 这里支持多种寻址方式:classpath和file -->  
              <value>classpath:/opt/demo/config/demo-db.properties</value>  
              <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
              <value>file:/opt/demo/config/demo-mq.properties</value>  
              <value>file:/opt/demo/config/demo-remote.properties</value>  
            </list>  
        </property>  
    </bean>  

相关文章

网友评论

      本文标题:spring加载外部属性文件进行数据库连接

      本文链接:https://www.haomeiwen.com/subject/hklbrktx.html