Spring整合Mybatis出现Access denied for user 'Think'@'localhost' (using password: YES)

Spring整合Mybatis出现Access denied for user 'Think'@'localhost' (using password: YES)

在整合mybatis时出现Access denied for user 'Think'@'localhost' (using password: YES)

找了好半天,网上好多改密码。发现没什么用,可能是我问题描述得不清楚。

后来看了小小酥512才知道问题

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456

spring-dao.xml

<context:property-placeholder location="db.properties" system-properties-mode="NEVER"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

问题原因:

在系统中也有个username属性,这时系统变量覆盖了Properties中的值,这时取得username的值为系统的用户名Administrator,密码为properties中的password去查询数据库,此时用户名名和密码并不匹配就会报错。在Spring完成注入时是用 "${..}" 方式获取值完成注入的。而通过这种表达式也能直接获取到JVM系统属性..........

解决方案:

方案一:

将properties文件中的username换成user或其他就字符串就可以成功获取连接访问数据库。建议:username时敏感词汇,为了安全起见还是尽量不要使用username。

方案二:

在Spring配置文件中修改成:<context:property-placeholder location="classpath:/db.properties" system-properties-mode="NEVER"/> 添加一个system-properties-mode属性

该属性有4个属性:

  • ENVIRONMENT”:表示占位符应该针对当前环境和任何本地属性进行解析;(从Spring 3.1开始

    属性值默认为“ENVIRONMENT”)

  • “NEVER”:表示占位符只针对本地属性解析,而不针对系统属性解析;

  • “FALLBACK”:表示占位符应该针对任何本地属性进行解析,然后针对系统属性进行解析;

  • “OVERRIDE”:表示占位符应该首先针对系统属性进行解析,然后针对任何本地属性进行解析;      

原文:

Controls how to resolve placeholders against system properties. As of Spring 3.1, this
	attribute value defaults to "ENVIRONMENT", indicating that resolution of placeholders
	against system properties is handled via PropertySourcesPlaceholderConfigurer and its
	delegation to the current Spring Environment object.
	
For maximum backward compatibility, this attribute is preserved going forward with the
3.1 version of the context schema, and any values other than the default "ENVIRONMENT"
will cause a traditional PropertyPlaceholderConfigurer to be registered instead of the
newer PropertySourcesPlaceholderConfigurer variant. In this case, the Spring Environment
and its property sources are not interrogated when resolving placeholders. Users are
encouraged to consider this attribute deprecated, and to take advantage of the
Environment and PropertySource mechanisms. See ConfigurableEnvironment javadoc for examples.

"ENVIRONMENT" indicates placeholders should be resolved against the current Environment and against any local properties;
"NEVER" indicates placeholders should be resolved only against local properties and never against system properties;
"FALLBACK" indicates placeholders should be resolved against any local properties and then against system properties;
"OVERRIDE" indicates placeholders should be resolved first against system properties and then against any local properties;

机翻:

控制如何根据系统属性解析占位符。从Spring 3.1开始

属性值默认为“ENVIRONMENT”,表示占位符的解析

对系统属性的处理是通过PropertySourcesPlaceholderConfigurer及其

委托给当前Spring环境对象。

为了获得最大的向后兼容性,此属性在向前执行时保留

3.1上下文模式的版本,以及除默认“环境”之外的任何值

会导致注册一个传统的PropertyPlaceholderConfigurer而不是

新PropertySourcesPlaceholderConfigurer变体。在本例中,是Spring环境

在解析占位符时,不会询问它的属性源。用户

建议考虑不赞成使用此属性,并利用

环境和属性源机制。有关示例,请参见ConfigurableEnvironment javadoc。

“ENVIRONMENT”:表示占位符应该针对当前环境和任何本地属性进行解析;

“NEVER”:表示占位符只针对本地属性解析,而不针对系统属性解析;

“回退”:表示占位符应该针对任何本地属性进行解析,然后针对系统属性进行解析;

“OVERRIDE”:表示占位符应该首先针对系统属性进行解析,然后针对任何本地属性进行解析;

原文地址:https://www.cnblogs.com/whitespaces/p/12442268.html