In Spring Boot, you can access values defined in the application.properties (or application.yml) file using the @Value annotation, which allows you to inject the value into your Spring components.

Using @Value annotation:

Here’s an example of how to access a value defined in the application.properties file:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    
    @Value("${my.property}")
    private String myPropertyValue;

    public void doSomething() {
        System.out.println("my.property value is: " + myPropertyValue);
    }
}

In this example, we have a Spring component called MyComponent that uses the @Value annotation to inject the value of the my.property key from the application.properties file. The ${my.property} syntax is used to reference the value of the key in the application.properties file.

Using Environment to read dynamically

You can also access values defined in the application.properties file using the Environment object, which is available as a bean in the Spring context. Here’s an example of how to access a value using the Environment object:

eimport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    
    @Autowired
    private Environment environment;

    public void doSomething() {
        String myPropertyValue = environment.getProperty("my.property");
        System.out.println("my.property value is: " + myPropertyValue);
    }
}

In this example, we are injecting the Environment object into the MyComponent class using the @Autowired annotation. We then use the getProperty method of the Environment object to retrieve the value of the my.property key from the application.properties file.

These are just a couple of examples of how to access values defined in the application.properties file in Spring Boot. There are other ways to access these values depending on your specific use case, but the @Value annotation and the Environment object are two of the most common methods.