英文:
Spring confiuration reading incorrect value from the properties file while creating JDBC connection
问题
我有一个类似于以下样式的 Spring 配置 bean:
@Configuration
@PropertySource("classpath:db/jdbc2.properties")
public class DbConfig {
@Value("${driverClassName}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@SuppressWarnings("unchecked")
@Lazy
@Bean
public DataSource dataSource() {
try {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
dataSource.setDriverClass(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
} catch (Exception e) {
return null;
}
}
}
jdbc2.properties 文件如下所示:
driverClassName=com.mysql.cj.jdbc.Driver
#url=jdbc:mysql://localhost:3306/musicdb?useSSL=true
url=jdbc:mysql://localhost:3306/musicdb
username=prospring6
password=prospring6
然而,当我尝试获取 JDBC 连接时,出现以下错误:
java.sql.SQLException: Access denied for user 'Delta'@'localhost' (using password: YES)
尽管在属性文件中将 username 字段设置为 prospring6,但配置将其作为我的系统用户名 Delta 进行读取。
英文:
I have a Spring Configuration bean which looks like this
@Configuration
@PropertySource("classpath:db/jdbc2.properties")
public class DbConfig {
@Value("${driverClassName}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@SuppressWarnings("unchecked")
@Lazy
@Bean
public DataSource dataSource() {
try {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
dataSource.setDriverClass(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
} catch (Exception e) {
return null;
}
}
}
The jdbc2.properties file looks like this
driverClassName=com.mysql.cj.jdbc.Driver
#url=jdbc:mysql://localhost:3306/musicdb?useSSL=true
url=jdbc:mysql://localhost:3306/musicdb
username=prospring6
password=prospring6
However, when I run try to get JDBC connection, I get the following error
java.sql.SQLException: Access denied for user 'Delta'@'localhost' (using password: YES)
Even though, username field is set prospring6 in the properties file, the configuration is reading it as Delta, which is my system's username.
答案1
得分: 1
如果您使用的是Windows,那么可能就是这个问题。
https://docs.oracle.com/javase/tutorial/essential/environment/env.html
(请参阅平台依赖性问题部分)
将属性键从"username"更改为"db.username"(它应该是任何内容,但不仅限于"username")... 可能会开始工作。
英文:
If you are using windows then might be this is the issue.
https://docs.oracle.com/javase/tutorial/essential/environment/env.html
(See the platform dependency issue section)
Changed the property key from username to db.username (it should be anything but not only username)... might be it will starts working.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论