How to Access a Value Defined in the Application.Properties File in Spring Boot

How to access a value defined in the application.properties file in Spring Boot

You can use the @Value annotation and access the property in whichever Spring bean you're using

@Value("${userBucket.path}")
private String userBucketPath;

The Externalized Configuration section of the Spring Boot docs, explains all the details that you might need.

JAVA Spring Boot : How to access application.properties values in normal class

There's no "magic" way to inject values from a property file into a class that isn't a bean. You can define a static java.util.Properties field in the class, load values from the file manually when the class is loading and then work with this field:

public final class AppUtils {
private static final Properties properties;

static {
properties = new Properties();

try {
ClassLoader classLoader = AppUtils.class.getClassLoader();
InputStream applicationPropertiesStream = classLoader.getResourceAsStream("application.properties");
applicationProperties.load(applicationPropertiesStream);
} catch (Exception e) {
// process the exception
}
}
}

How to access defined in the application.properties file in Spring Boot?

You can use the @Value annotation and access the property in whichever Spring bean you're using

@Value("${userBucket.path}")
private String userBucketPath;

How to read the application.properties file from a location outside the spring boot application

lets say, your application requires externalized properties like application.properties and another property file myapp.properties. The both properties can be in the same folder or they can be in different folder. There are 3 ways of doing it.

Command line arguments

In the first approach, all you need to do is pass folder names and property names as part of command line arguments as shown below:

java -jar myapp.jar --spring.config.name=application,myapp
--spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config

Environment variables

In the second approach, you can configure your externalized configuration details into environment variables and your Spring Boot application will read it from your environment as shown below:

set SPRING_CONFIG_NAME=application,myapp

set SPRING_CONFIG_LOCATION=classpath:/data/myapp/config,classpath:/data/myapp/external/config

java -jar myapp.jar

Programatically loding configurations

package com.java2novice.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

@SpringBootApplication
public class SpringBootWebApplication {

private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);

public static void main(String[] args) throws Exception {

ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootWebApplication.class)
.properties("spring.config.name:application,myapp",
"spring.config.location:classpath:/data/myapp/config,classpath:/data/myapp/external/config")
.build().run(args);

ConfigurableEnvironment environment = applicationContext.getEnvironment();

logger.info(environment.getProperty("cmdb.resource-url"));
}
}


Related Topics



Leave a reply



Submit