Accessing properties files programmatically in a Spring application can be done using the @Value annotation or the Environment object provided by Spring. Here, We'll show both methods for accessing properties from a properties file.
@Value AnnotationThe @Value annotation is the simplest way to inject properties from a properties file into your Spring beans.
Create a Properties File
Create a file named application.properties (or application.yml for YAML) in the src/main/resources directory.
app.name=MySpringApp app.version=1.0.0
Access Properties using @Value
Create a Spring component and use the @Value annotation to inject the properties.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyAppProperties { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; public String getAppName() { return appName; } public String getAppVersion() { return appVersion; } } Use the Component in Your Application
Autowire the component into your application and use the properties.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication implements CommandLineRunner { @Autowired private MyAppProperties myAppProperties; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("App Name: " + myAppProperties.getAppName()); System.out.println("App Version: " + myAppProperties.getAppVersion()); } } Environment ObjectThe Environment object is another way to access properties, providing a more programmatic approach.
Create a Properties File
(Same as above)
Access Properties using Environment
Autowire the Environment object into a component or service.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class MyAppProperties { @Autowired private Environment env; public String getAppName() { return env.getProperty("app.name"); } public String getAppVersion() { return env.getProperty("app.version"); } } Use the Component in Your Application
(Same as above)
@Value Annotation: Best for simple property injections where the property names are known and don't change.Environment Object: Provides a more flexible and programmatic way to access properties, allowing for conditional logic and dynamic property retrieval.Both methods can be used in a Spring Boot application to access properties from a properties file. Choose the one that best fits your needs based on the complexity and requirements of your application.
How to load properties file in Spring Boot programmatically?
PropertiesLoaderUtils.import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.util.Properties; public class PropertyLoader { public void loadProperties() { try { ClassPathResource resource = new ClassPathResource("application.properties"); Properties props = PropertiesLoaderUtils.loadProperties(resource); String value = props.getProperty("key"); System.out.println("Value from properties file: " + value); } catch (IOException e) { e.printStackTrace(); } } } Spring access properties file outside classpath
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.util.Properties; public class PropertyLoader { public void loadProperties() { try { FileSystemResource resource = new FileSystemResource("/path/to/external.properties"); Properties props = PropertiesLoaderUtils.loadProperties(resource); String value = props.getProperty("key"); System.out.println("Value from properties file: " + value); } catch (IOException e) { e.printStackTrace(); } } } Spring read properties file from environment variable
import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.util.Properties; public class PropertyLoader { private Environment environment; public PropertyLoader(Environment environment) { this.environment = environment; } public void loadProperties() { try { String path = environment.getProperty("PROPERTIES_FILE_PATH"); ClassPathResource resource = new ClassPathResource(path); Properties props = PropertiesLoaderUtils.loadProperties(resource); String value = props.getProperty("key"); System.out.println("Value from properties file: " + value); } catch (IOException e) { e.printStackTrace(); } } } Spring load properties file from specific location
ResourceLoader.import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.util.Properties; public class PropertyLoader { private ResourceLoader resourceLoader; public PropertyLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public void loadProperties() { try { String path = "/path/to/external.properties"; Properties props = PropertiesLoaderUtils.loadProperties(resourceLoader.getResource("file:" + path)); String value = props.getProperty("key"); System.out.println("Value from properties file: " + value); } catch (IOException e) { e.printStackTrace(); } } } Spring Boot load properties file from classpath
@PropertySource annotation.import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:application.properties") public class PropertyLoader { @Value("${key}") private String value; public void printPropertyValue() { System.out.println("Value from properties file: " + value); } } Spring read properties file with placeholder configuration
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class PropertyLoader { @Value("${path.to.properties}") private String path; public void loadProperties() { // Use path variable to load properties file // Implementation depends on the specific use case } } Spring access YAML properties programmatically
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "config") public class YamlPropertyLoader { private String key; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public void printPropertyValue() { System.out.println("Value from YAML properties: " + key); } } Spring Boot access multiple properties files programmatically
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource({ "classpath:application.properties", "classpath:additional.properties" }) public class MultiPropertyLoader { @Value("${key}") private String value; public void printPropertyValue() { System.out.println("Value from properties file: " + value); } } Spring Boot access encrypted properties programmatically
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.vault.core.VaultTemplate; import org.springframework.vault.support.VaultResponseSupport; @Component public class EncryptedPropertyLoader { private final VaultTemplate vaultTemplate; @Value("${secret.property.path}") private String secretPropertyPath; public EncryptedPropertyLoader(VaultTemplate vaultTemplate) { this.vaultTemplate = vaultTemplate; } public String getSecretProperty() { VaultResponseSupport<SecretProperties> response = vaultTemplate.read(secretPropertyPath, SecretProperties.class); return response.getData().getPropertyValue(); } } Spring Boot access properties file from different environments
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:${environment}-application.properties") public class EnvironmentSpecificPropertyLoader { @Value("${key}") private String value; public void printPropertyValue() { System.out.println("Value from properties file: " + value); } } jwt service-worker alpine-linux cors parent-child docker-desktop transpose sympy linear-interpolation parent-pom