I want to configure a tomcat data source in Spring Boot, The properties of the database are stored in another property file (Say dbConnection.properties) with different keys.
For example
dbConnection.properties:
DATABASE_URL=SomeURL
DATABASE_USER=SomeUser
DATABASE_PASSWORD=SomePassword
From what I understand the properties related to a data source must be specified in application.properties as:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
How do I pass the values from dbConnection.properties to application.properties?
From Spring Boot documentation
Property contributions can come from additional jar files on your classpath so you should not consider this an exhaustive list. It is also perfectly legit to define your own properties.
so you can have your own property file and it should be in your classpath,
Inject property using Value
annotation
@Value("#{propFileName.propKeyName}")
All you need is override the Spring-Boot's Datasource default configuration. See the example above:
@Bean
@Primary
public DataSource dataSource() {
return DataSourceBuilder
.create()
.username("") // TODO: Get from properties
.password("") // TODO: Get from properties
.url("") // TODO: Get from properties
.driverClassName("") // TODO: Get from properties
.build();
}
In order to get from properties you can use the @Value
annotation that @Saravana said you.
@Manish Kothari... try this,create a configuration class with the annotations like @ConfigurationProperties.
@Component
@PropertySource("classpath:dbConnection.properties")
@ConfigurationProperties
public class ConfigurationClass{
//some code
}
and now it will call your DB properities... I hope this will work
There are multiple ways to do this 1.You can pass the property files from the command promopt
-Dspring.config.location=classpath:job1.properties,classpath:job2.properties
2.Way is to add @PropertySource annotation public class AppConfig
@PropertySource("classpath:config.properties")
public class LoadDbProps{
@value("${DATABASE_USER}")
private String dbUserName;
private String dbUserName;
}
Later you can set this LoadDbProps to application.properties properties using @Bean configuration.
The below solution worked for me :
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
Properties properties = null;
InputStream inputStream = null;
DataSourceBuilder dataSourceBuilder =null;
try {
properties = new Properties();
inputStream = new FileInputStream("./src/main/resources/config.properties");
properties.load(inputStream);
dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(properties.getProperty("url"));
dataSourceBuilder.username(properties.getProperty("user"));
dataSourceBuilder.password(properties.getProperty("password"));
}
catch(Exception ex) {
System.out.println("CONFIG EXCEPTION :"+ex);
}
return dataSourceBuilder.build();
}
}
refer below link for more details:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.