The Reedemed77
The Reedemed77

Reputation: 459

Read values from yaml file

I have the following issue. I create a data source based on a value I read in the yaml file based on a given profile.

Here is my code

@Value("${my.db.serviceId}")
private String serviceId;

@Primary
@Bean(name = "prodDataSource")
@Profile("prod")
public DataSource prodDataSource() {
    return getDataSource(serviceId);
}

@Bean(name = "devDataSource")
@Profile("dev")
public DataSource devDataSource() {
    return getDataSource(serviceId);
}

Here is my yaml file

---

spring:
   profile: dev
my:
  db:
    serviceId: 'my-dev-service'
---

spring:
  profile: prod
my:
 db:
   serviceId: 'my-prod-service'

---

My current issue is that when I start my application with the "dev" profile, the value of the serviceId is 'my-prod-service'.

What am I doing wrong here?

Upvotes: 0

Views: 3315

Answers (2)

The Reedemed77
The Reedemed77

Reputation: 459

so I finally realized that in the yaml file I put "profile" instead of "profiles". that's why it wasn't picking up my profile.

I endeded up changing to:

---

spring:
   profiles: dev
my:
 db:
  serviceId: 'my-dev-service'
---

spring:
   profiles: prod
my:
 db:
  serviceId: 'my-prod-service'

---

Upvotes: 0

Ryuzaki L
Ryuzaki L

Reputation: 40078

@Primary annotation enables a bean that gets preference when more than one bean is qualified to autowire a single valued dependency

So the bean with @Primary annotation will get more preference

Upvotes: 1

Related Questions