项目场景:
项目场景
:在自学spring ioc使用JdbcTemplate+Druid连接池中出现了连接不上数据库的问题,原因是数据库的用户名被识别为了系统的用户名。
问题描述
配置spring的xml配置文件读取druid的properties配置文件时,其他参数都能正确识别,唯独username会识别为当前系统的用户名
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取外部.properties配置文件 => 设置properties标签,其中value = "${key}"
<context:property-placeholder location="classpath:druid.properties",classpath:其他配置,可以添加多个properties配置文件/>
-->
<context:property-placeholder location="classpath:druid.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
# druid.properties
url = jdbc:mysql://localhost:3306/studb
username = user
password = xxxx
driverClassName = com.mysql.cj.jdbc.Driver
initialSize = 5
maxActive = 20
原因分析:
既然username被识别为了系统的用户名,说明spring读取了系统变量的USERNAME
在cmd中输入set查看环境变量
查阅资料发现,spring默认会优先加载系统环境变量,此时获取到的username的值实际上指的是当前计算机的用户名。而不是properties配置文件中指定的username的值。
解决方案:
方案一:修改配置文件,不要使用与环境变量相同的key,比如把username改为db.username
# druid.properties
url = jdbc:mysql://localhost:3306/studb
db.username = user
password = xxxx
driverClassName = com.mysql.cj.jdbc.Driver
initialSize = 5
maxActive = 20
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取外部.properties配置文件 => 设置properties标签,其中value = "${key}"
<context:property-placeholder location="classpath:druid.properties",classpath:其他配置,可以添加多个properties配置文件/>
-->
<context:property-placeholder location="classpath:druid.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
方案二:修改spring的xml配置文件,设置local-override=“true”,覆盖重名的环境变量
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取外部.properties配置文件 => 设置properties标签,其中value = "${key}"
<context:property-placeholder location="classpath:druid.properties",classpath:其他配置,可以添加多个properties配置文件/>
-->
<!-- 设置local-override="true",覆盖重名的环境变量,否则username撞系统变量了 -->
<context:property-placeholder local-override="true" location="classpath:druid.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>