Logo

Using envvault with Java/Spring Boot

Note: Please ensure that you have completed the previous steps

Prerequisites

  • Java Development Kit (JDK) installed
  • envvault CLI tool installed
  • An existing Spring Boot project

Usage

Running Your Application

To run your Spring Boot application with environment variables from envvault:

$ envvault run --env=dev ./mvnw spring-boot:run

Caching Environment Variables

For better performance, you can cache your environment variables:

$ envvault run --env=dev -c -- ./mvnw spring-boot:run

Example Implementation

Here is how to set up a basic Spring Boot application with envvault:

// Application.java
package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {
    // Your environment variables are automatically loaded
    @Value("${PORT:8080}")
    private String port;

    @Value("${DB_URL}")
    private String dbUrl;

    @GetMapping("/")
    public String hello() {
        return "Hello from envvault!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

// application.properties
server.port=${PORT:8080}
spring.datasource.url=${DB_URL}