Raj
Raj

Reputation: 125

Multiple properties in YAML file Spring Boot

spring: 
   profiles: dev
spring.datasource: 
        driver-class-name: 
        password: ~
        url: ~
        username: ~
--- 
secdb: 
  profiles: dev
spring.datasource: 
       driver-class-name: ~
       password: ~
       url: ~
       username: ~
---

I have above two properties declared as shown in the application.yml file but when I use it in implementation class as follows.

@Value("${spring.datasource.url}")
private String URL;

it works and picks up the url from YML file. but when I do as follows

@Value("${secdb.spring.datasource.url}")
private String URL;

it fails at spring boot start saying

Could not resolve placeholder 'secdb.spring.datasource.url' in value...

As, I am at beginner level. YML may be wrong but my intention is to have two data sources in the YML file and use the second one for one JDBC connection other one is default. Please, guide me through the mistake

Upvotes: 2

Views: 6105

Answers (1)

Jason
Jason

Reputation: 106

You have made two mistakes in your yaml file.

  1. Don't use space before ---.
  2. Before your first spring.datasource:, there is a space. It indicates spring.datasource: is a subproperty of spring:.
  3. @Value("${secdb.spring.datasource.url}") is absolutely not the right way. Even you active secdb, you also need to get the value like @Value("${spring.datasource.url}").
  4. I do't suggest you to use Spring profiles like secdb: profiles: dev. It's not a familiar way. You can use it like spring: profiles: secdb and active it just like spring.profiles.active=secdb. Or if you insist to use it that way, you need to active it like spring.profiles.active=secdb.

After all, if you want to use Spring profiles properties, you need to active it just like $ java -jar -Dspring.profiles.active=production or add spring.profiles.active=production in application.properties.

I suggest you to read this document in detail.

I will be glad if it helps.

Upvotes: 3

Related Questions