Reputation: 65
@PropertySource not picking the correct property file
Here is my complete code :
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
Both the properties files are available inside src/main/resources directory
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());
}
}
And here is the output which is same for both version-1 and version-2 as mentioned in DataSourceConfig.java file
jdbc:oracle://localhost:3306/Prod
OracleDriver
Prod
ProdPassword
It shows that the @PropertySource annotation used in DataSourceConfig class is not picking the correct properties file which is "db.properties" file
How can i read properties from db.properties file in this case.
Upvotes: 0
Views: 1780
Reputation: 893
Properties files are read in very particular order as mentioned here.
So application.properties
takes precedence over the properties you are reading via @PropertySource
and as a result, your db.properties
is being overwritten as both properties files have the same keys.
Change the property keys
in your db.properties
and it should work fine.
Upvotes: 1