绑定在application.yml中的属性不起作用。

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

Bind properties in application.yml not working

问题

你的问题是关于SpringBoot项目中绑定application.yml文件中的属性到Java类时出现了问题。错误信息指出driverClassName属性为空,因此绑定失败。以下是你可能遗漏的一些问题:

  1. 确保application.yml中的属性命名和Java类中的属性命名一致,包括大小写。

  2. 确保DBConfig类上的@ConfigurationProperties注解引用的前缀与application.yml中的前缀一致。在你的代码中,前缀是"spring.datasource"

  3. 检查application.yml文件是否正确放置在src/main/resources/目录下。

  4. 确保项目的依赖项正确配置,包括Spring Boot Starter和SQLite数据库驱动。

  5. 如果你在application.yml中使用了敏感字符,例如",请确保它们被正确解析为双引号。

  6. 确保DBConfig类被Spring Boot正确扫描并成为应用程序上下文的一部分。

最后,确保SQLite在Spring Boot中的配置是正确的,因为它可能需要特定的配置和依赖项来正确运行。如果SQLite在Spring Boot中的支持不够好,你可能需要手动配置数据源,就像你在DBConfig中所做的那样。

英文:

In my SpringBoot project I have application.yml file in src/main/resources/:

server:
  port: 8080

spring:
  datasource:
    driver-class-name: org.sqlite.JDBC
    ...

I want to bind the properties defined above to a Java class:

@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DBConfig {

    // I wished spring-boot could automatically bind it with 'driver-class-name' in application.yml
    private String driverClassName;
    ...

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        ...
        return dataSource;
    }
}

I have the annotation @Configuration and @ConfigurationProperties(prefix = "spring.datasource"). I also have specified the correct prefix. But when I run my application I get an error:

> Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/my/webapp/config/DBConfig.class]: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception with message: Property 'driverClassName' must not be empty

The error tells driverClassName is an empty string, so the binding doesn't work. What am I missing? I am using an SQLite database which is not as well supported as MySQL, MariaDB, etc. in SpringBoot. So I provide a datasource bean class to bind database configurations in application.yml to the manually created bean class. Is it unnecessary?

答案1

得分: 0

通常配置属性的常规方式是将它们保存在单独的类中,并使用 @EnableConfigurationProperties 来激活它们。您可以阅读这篇文章。您可以尝试以下内容:

@Configuration
@EnableConfigurationProperties(MyConfigurationProperties.class)
public class DBConfig {

    @Autowired
    private MyConfigurationProperties props;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(props.getDriverClassName());
        // ...
        return dataSource;
    }
}

而您的属性类将如下所示:

@ConfigurationProperties(prefix = "spring.datasource")
public class MyConfigurationProperties {

    private String driverClassName;

    // 这里有getter和setter方法
}

但我不确定这是否是最佳解决方案,因为Spring已经有了自己的配置属性类,它绑定到前缀 spring.datasource

第二个选项是使用Spring配置属性:

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
// ...

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DBConfig {

    @Autowired
    private DataSourceProperties props;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(props.getDriverClassName());
        // ...
        return dataSource;
    }
}
英文:

The usual way for configuration properties is to keep them in the separate class and activate them using @EnableConfigurationProperties. You can read this article. And you can try something as follows:

@Configuration
@EnableConfigurationProperties(MyConfigurationProperties.class)
public class DBConfig {

    @Autowired
    private MyConfigurationProperties props;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(props.getDriverClassName());
        // ...
        return dataSource;
    }
}

And your properties class will look like this:

@ConfigurationProperties(prefix = "spring.datasource")
public class MyConfigurationProperties {
    
    private String driverClassName;

    // getters ans setters are here
}

But I'm not sure if this is the best solution, because Spring already has its own configuration properties class that binds to the prefix spring.datasource.

And the second option for you is to use the Spring configuration properties:

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
// ...

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DBConfig {

    @Autowired
    private DataSourceProperties props;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(props.getDriverClassName());
        // ...
        return dataSource;
    }
}

答案2

得分: 0

你只需要为你的 DBConfig 类中的字段添加设置器,它就会正常工作。

英文:

You only need to add setters for feilds in your DBConfig class and it will work.

huangapple
  • 本文由 发表于 2023年8月4日 21:47:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836550.html
匿名

发表评论

匿名网友

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

确定