Using Env Variable in Spring Boot's Application.Properties

spring boot .env variables in application.properties

This won't happen automatically. You can make it work by adding this to your application-dev.properties:

spring.config.import=optional:file:.env[.properties]

Another option is to use an additional library that adds support for this, such as https://github.com/paulschwarz/spring-dotenv.

There is some discussion about adding support for this in Spring Boot, but it is not a high priority.

environment variables in Spring-Boot in email data in application.properties

It should work, but you doesn't specify what is not working. To make these properties useful you have to wire the right Spring Boot plugin and obviously use it in your code:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.7.0</version>
</dependency>
// wire it in your bean
@Autowired
private JavaMailSender emailSender;
// reference the emailSender and send emails ...

Using system variable in Spring Boot's application.properties

Of course you can read environment variables from properties file. Provided username is configured as environment variable in the system you want to execute the code on.

simply replace,

this: logfile.dir=/Users/Nunito/logs/icrypts/

with: logfile.dir=/Users/${userName}/logs/icrypts/

You can also read properties file using any of below ways.

@Value("${username}")
private String username;

You can also use, SPRING_APPLICATION_JSON

At application startup, Spring Boot will look for an environment variable called SPRING_APPLICATION_JSON. It can be used to provide a set of application properties using inline JSON. For example, you can set ec2.public.url property as follows.

You can refer samples from here,
https://github.com/indrekots/spring-boot-envvariables



Related Topics



Leave a reply



Submit