-1

I have two spring profile files application-test.yml and application-prod.yml, with some properties in each file. I have set

spring:
  profiles:
     active: test, prod

in my application.yml file and added the @Profile(value= ["test", "prod"]) in my configuration file.

Now, my question is how to read the content of the two files and what do I need to pass in @Value?? This is how I implemented it:

@Profile(value= ["test", "prod"])
@Configuration()
class myMessege(

@Value("${"??????"}") 
private val newMessege: String) {
    
@Bean
fun readMessage {
     println(newMessege)
}

The application-test.yml is

spring.profiles: test 

key:
- name: tom
  roles:
    - student
    - teacher
    - friend

and the application-prod.yml is

spring.profiles: test 

key:
- name:lisa
  roles:
    - doctor
    - nurse
    - patient

How to return the key from both files?

Kate
  • 11
  • 5
  • What do you want the value of `newMessege` to be? what is returned from `readMessage`? – Abhijit Sarkar Oct 30 '22 at 04:35
  • I want the value of newMessege to be whatever defined in both application-test.yml and application-prod.yml. Lets say I have name: tom in application-test.yml and name: lisa in application-prod.yml and I want the readMessage return tom lisa – Kate Oct 30 '22 at 05:40
  • That’s not how properties work in spring. – Abhijit Sarkar Oct 30 '22 at 05:41
  • @AbhijitSarkar how to read the key value from both files? Please see the description I have added application-test.yml and appliation.prod.yml Also, I would appreciate it if you can give me more details how properties are working in spring. – Kate Oct 30 '22 at 06:11

1 Answers1

0
  1. Properties in Spring operate in the same namespace so a property identifier like "key.name" in application-test.yml identifies the same thing as application-prod.yml
  2. If properties are declared twice, the last declared properties file wins (or overrides) - think of this like CSS
  3. If you want additive settings (e.g. names from different files all get rolled into the same property then you'll have to do this yourself, e.g. by naming the property "key.test.name" and "key.prod.name" and then look for these in code and merge them.
AndrewL
  • 2,034
  • 18
  • 18