sping配置文件中引入properties文件方式

时间:2023-03-09 18:40:46
sping配置文件中引入properties文件方式
<!-- 用于引入 jdbc有配置文件参数 可以使用PropertyPlaceholderConfigurer 或者PropertyOverrideConfigurer -->
<bean class="org.springframework.beans.factory.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:db.properties</value>
</property>
</bean>

其中的<properties name="location">中的name必须是location,别的不行,我试了。

然后我们就在配置dataSource的时候通${key}的方式来调用了

<!-- 利用上面导入的 配置文件配置 数据源  
  连接池的作用是可以不用经常去获得一个连接
  最主要的是连接可重得使用使得性能有很大的提升
  -->

db.properties文件

#mysql
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost/mybatis?characterEncoding=utf-8
dialect=org.hibernate.dialect.MySQL5Dialect
user=root
password=root
show_sql=true
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClass}"></property>
<property name="url" value="${jdbcUrl}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>