Spring配置在创建JDBC连接时从属性文件中读取的值不正确。

huangapple go评论77阅读模式
英文:

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(&quot;classpath:db/jdbc2.properties&quot;)
public class DbConfig {

	@Value(&quot;${driverClassName}&quot;)
	private String driverClassName;
	@Value(&quot;${url}&quot;)
	private String url;
	@Value(&quot;${username}&quot;)
	private String username;
	@Value(&quot;${password}&quot;)
	private String password;

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@SuppressWarnings(&quot;unchecked&quot;)
	@Lazy
	@Bean
	public DataSource dataSource() {
		try {
			SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
			Class&lt;? extends Driver&gt; driver = (Class&lt;? extends Driver&gt;) 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 &#39;Delta&#39;@&#39;localhost&#39; (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.

huangapple
  • 本文由 发表于 2020年10月10日 12:45:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64290065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定