@PropertySource 没有选择正确的 属性 文件

@PropertySource not picking the correct property file

@PropertySource 没有选择正确的 属性 文件

这是我的完整代码:

pom.xml

    <groupId>org.example</groupId>
    <artifactId>PropertySource</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>14</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

application.properties

connection_Url=jdbc:oracle://localhost:3306/Prod
driver_Class=OracleDriver
user_Name=Prod
password=ProdPassword

db.properties

connection_Url=jdbc:MySQl://localhost:3306/Test
driver_Class=MySQLDriver
user_Name=Test
password=TestPassword

两个属性文件都在 src/main/resources 目录

中可用

DataSource.java

public class DataSource {

    private String connectionURL;
    private String driverClass;
    private String userName;
    private String password;

    getters/setters
}

DataSourceConfig.java

@Configuration
@PropertySource(value = "classpath:db.properties", ignoreResourceNotFound = true)
public class DataSourceConfig {

    //    Version-1

    @Autowired
    Environment environment;

    @Bean
    public DataSource getDataSource() {
        DataSource dataSource = new DataSource();
        dataSource.setConnectionURL(environment.getProperty("connection_Url"));
        dataSource.setDriverClass(environment.getProperty("driver_Class"));
        dataSource.setUserName(environment.getProperty("user_Name"));
        dataSource.setPassword(environment.getProperty("password"));
        return dataSource;
    }

    //    Version-2

    /*
    @Value("${connection_Url}")
    private String connectionURL;

    @Value("${driver_Class}")
    private String driverClass;

    @Value("${user_Name}")
    private String userName;

    @Value("${password}")
    private String password;

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

    @Bean
    @Autowired
    public DataSource getDataSource() {
        DataSource dataSource = new DataSource();
        dataSource.setConnectionURL(connectionURL);
        dataSource.setDriverClass(driverClass);
        dataSource.setUserName(userName);
        dataSource.setPassword(password);
        return dataSource;
    }*/
}

Application.java

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        DataSource dataSource = context.getBean(DataSource.class);

        System.out.println(dataSource.getConnectionURL());
        System.out.println(dataSource.getDriverClass());
        System.out.println(dataSource.getUserName());
        System.out.println(dataSource.getPassword());
    }
}

这里的输出与 DataSourceConfig.java 文件

中提到的版本 1 和版本 2 相同
jdbc:oracle://localhost:3306/Prod
OracleDriver
Prod
ProdPassword

这表明 DataSourceConfig class 中使用的 @PropertySource 注释没有选择正确的属性文件,即“db.properties”文件

在这种情况下,我如何从 db.properties 文件中读取属性。

如前所述 here

以非常特殊的顺序读取属性文件

因此 application.properties 优先于您通过 @PropertySource 读取的属性,因此,您的 db.properties 被覆盖,因为两个属性文件具有相同的键。

更改 db.properties 中的 属性 keys,它应该可以正常工作。