变量命名冲突的解决思路(spring框架中使用${}动态引入用户名,想连接数据,结果访问被拒绝)

简介: 变量命名冲突的解决思路(spring框架中使用${}动态引入用户名,想连接数据,结果访问被拒绝)

【在spring框架中使用${}动态引入用户名】访问被拒绝:Access denied for user 'Hua'@'localhost' (using password: YES)

88.png

java.lang.IllegalStateException: Failed to load ApplicationContext
  at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
  at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
  at ...
Caused by: java.sql.SQLException: Access denied for user 'Hua'@'localhost' (using password: YES)....


代码如下:

■ 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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        ">
     <!-- 从classpath的根路径 加载db.properties -->   
     <context:property-placeholder location="classpath: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="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
    <property name="initialSize" value="${initialSize}"/>
  </bean>
</beans>

■ 测试类:

@SpringJUnitConfig
public class App {
  @Autowired
  private DruidDataSource ds;
  @Test
  void test1() throws Exception {
//    ds = new DruidDataSource();
//    ds.setDriverClassName("com.mysql.jdbc.Driver");
//    ds.setUrl("jdbc:mysql://localhost:3306/springdemo?useSSL=false");
//    ds.setUsername("root");
//    ds.setPassword("admin");
    @Cleanup
    Connection connection = ds.getConnection();
    String sql = "select id, name, age from student";
    @Cleanup
    PreparedStatement ps = connection.prepareStatement(sql);
    @Cleanup
    ResultSet rs = ps.executeQuery();
    while(rs.next()) {
      System.out.print(rs.getLong("id") + ",");
      System.out.print(rs.getInt("age") + ",");
      System.out.println(rs.getString("name"));
    }
  }
}

● 分析思路:系统环境变量有个username,而数据库连接也有一个username,即出现同名了:

□ 解决同名思路1:通过设置属性,告诉配置不要引用系统环境变量的username,即system-properties-mode="NEVER"

89.png

✿ 那就不要出现同名的情况(从源头进行解决哈哈哈),连接数据库的变量名,命名为其他的

【一般情况,在sprig框架中,${username}动态引入的用户名默认是当前计算机系统的账户的用户名,不是数据库连接的用户名

目录
相关文章
|
12天前
|
存储 安全 Java
事件的力量:探索Spring框架中的事件处理机制
事件的力量:探索Spring框架中的事件处理机制
26 0
|
21天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
30 1
|
1月前
|
开发框架 安全 Java
Spring 框架:企业级应用开发的强大工具
在当今数字化时代,企业级应用开发的需求日益增长。为了满足这一需求,开发者们需要一款功能强大、易于使用的开发框架。Spring 框架作为 Java 领域的领先者,为企业级应用开发提供了全面的解决方案。本文将深入探讨 Spring 框架的各个方面,包括其历史、核心模块、优势以及应用场景。
23 0
|
1月前
|
存储 Java 数据库
|
1月前
|
存储 搜索推荐 Java
|
1月前
|
人工智能 JSON 前端开发
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
|
2月前
|
缓存 安全 Java
Shiro框架以及Spring Boot整合Shiro
Shiro框架以及Spring Boot整合Shiro
Shiro框架以及Spring Boot整合Shiro
|
15天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
1月前
|
Java 数据库连接 API
【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】
【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】
49 0
|
11天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
22 3

热门文章

最新文章