How do I use a variable from an application.yml file in my normal code?

17,647

Solution 1

You can use this:

@Value("${your.path.yml.string}")
private String x;

YML:

your:
  path:
    yml:
      string: hello

x will be "hello"

Solution 2

  1. You need to use Spring Expression Language which says we should write it as
    @Value("${spring.application.name}") 
    private String appName;
  1. For Default value if key is not present in yaml/yml or properties file
    @Value("${spring.application.name: defaultValue}") 
    private String appName;
  1. The last way you can fetch value is using environment object
    @Autowired  
    private Environment environment;
       
    String appName = environment.get("spring.application.name");

Solution 3

You can add @Value annotation to any field in your beans.

@Value("$(path.to.your.variable)")
String myString;

Works with constructors as well.

public MyClass(@Value("$(path.to.your.variable)") String myString) {

Solution 4

You can use @Value on fields or parameters to assign the property to some variable.

Property example:

@Value("${indicator}")
private String indicator

Parameter example:

private void someMethod(@Value("${indicator}") String indicator) {
    ...
}

Then you can use indicator as you want.

Note: the class where you use @Value should be a Spring Component

Share:
17,647

Related videos on Youtube

nolanite1
Author by

nolanite1

Updated on June 04, 2022

Comments

  • nolanite1
    nolanite1 almost 2 years

    For example, let's say that in my yml file I had a variable called indicator. And based on what the indicator variable's value was I want the code to do something different. How would I access the yml variable in the regular code and use it accordingly?

  • chrylis -cautiouslyoptimistic-
    chrylis -cautiouslyoptimistic- almost 4 years
    This entire functionality is one of the core things Spring provides in the first place.
  • Rishabh Sharma
    Rishabh Sharma almost 4 years
    It is not mentioned anywhere in the question that spring is required (inspite of the tag). I guess - "To a man with a hammer, everything looks like a nail" :)
  • nolanite1
    nolanite1 almost 4 years
    what would the path be like? just the path to the yaml file where the variable is?
  • chrylis -cautiouslyoptimistic-
    chrylis -cautiouslyoptimistic- almost 4 years
    It's tagged spring-boot and he mentions application.yml by name.
  • nolanite1
    nolanite1 almost 4 years
    oh ok. when i tried that i got an error saying Unsatisfied dependency expressed through field 'sampleService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'indicator.operation' in value "${indicator.string}
  • luisfa19
    luisfa19 almost 4 years
    use @Component in the class.
  • nolanite1
    nolanite1 almost 4 years
    wait the class i'm using it in or do i have to create a class for the variable?
  • luisfa19
    luisfa19 almost 4 years
    could be in the class you are using it