Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Microsoft – NPI EA (cat = Baeldung)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Microsoft – NPI EA (cat= Spring Boot)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, you can get started over on the documentation page.

And, you can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – All Access – NPI EA (cat= Spring)
announcement - icon

All Access is finally out, with all of my Spring courses. Learn JUnit is out as well, and Learn Maven is coming fast. And, of course, quite a bit more affordable. Finally.

>> GET THE COURSE
Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

End-to-end testing is a very useful method to make sure that your application works as intended. This highlights issues in the overall functionality of the software, that the unit and integration test stages may miss.

Playwright is an easy-to-use, but powerful tool that automates end-to-end testing, and supports all modern browsers and platforms.

When coupled with LambdaTest (an AI-powered cloud-based test execution platform) it can be further scaled to run the Playwright scripts in parallel across 3000+ browser and device combinations:

>> Automated End-to-End Testing With Playwright

Course – Spring Sale 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

Course – Spring Sale 2025 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll learn how to use Pkl (pronounced Pickle), a Configuration-as-Code language, to define configuration in Spring Boot applications.

Traditionally, we might define application configuration in YAML, JSON, or key-value-based property files. However, these are static formats, and validating the properties is challenging. Moreover, as the number of configuration properties grows defining more complex hierarchal configurations becomes increasingly difficult.

Therefore, using Configuration-as-Code (CaC), through special languages like Pkl, HCL (Hashicorp configuration language), and Dhal configuration language, can help us overcome the challenges of static property format files.

2. Introduction to Pkl

Configuration-as-Code is a popular concept that promotes the Don’t Repeat Yourself (DRY) principle and helps streamline configuration management. It adopts a declarative coding style, offering a structured and efficient approach to defining configuration templates. It also improves the readability of the configuration.

Pkl helps define various elements, such as object types, collections, maps, and a wide range of primitive data types. This flexibility makes the language extendable and helps model complex configurations easily with clarity and conciseness. Further, its type and validation mechanism helps catch configuration errors before application deployment.

Additionally, Pkl provides excellent tool support for easy adoption. It has tooling for generating code in different languages like Java, Kotlin, Go, and Swift. This is essential for embedding and reading Pickle configurations in applications built on these programming languages.

Further, IDEs like IntelliJ and VS Code have plugins to facilitate the development of configurations using Pkl. Pkl also comes with a CLI called pkl that helps evaluate pickle modules. One of its features is to convert .pkl configurations to JSON, YAML, and XML formats.

3. Spring Configuration Java Bean Binding

The most common approach to defining Spring configuration is to create property files and inject them using the @Value annotation. However, this often results in excessive boilerplate code.

Thankfully, the Spring Framework simplifies this process with the help of the @ConfigurationProperties annotation, enabling seamless binding of configurations to Java beans.

However, with this approach, we must still manually create the Java beans with any necessary validation on the member fields. As a result, configuration drift is difficult to detect and can often lead to critical bugs in the application. Therefore, a configuration definition language like Pkl with automated Java code generation support is a more robust way to integrate our application configuration.

4. Spring Boot Integration

Pkl offers libraries to generate POJOs from Pickle files. Later at runtime, the pkl-spring library can populate the Pickle configurations into the POJOs. Let’s learn how it can be integrated with a Spring Boot application.

4.1. Prerequisites

First, we’ll include the Maven dependencies for the pkl-spring library:

<dependency>
    <groupId>org.pkl-lang</groupId>
    <artifactId>pkl-spring</artifactId>
    <version>0.16.0</version>
</dependency>

Additionally, we’ll use the exec-maven-plugin to generate the POJOs from the Pickle configuration files:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>gen-pkl-java-bind</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>org.pkl.codegen.java.Main</mainClass>
                <includeProjectDependencies>false</includeProjectDependencies>
                <additionalClasspathElements>
                    <classpathElement>${pkl-tools-filepath}/pkl-tools-0.27.2.jar</classpathElement>
                </additionalClasspathElements>
                <arguments>
                    <argument>${project.basedir}/src/main/resources/ToolIntegrationTemplate.pkl</argument>
                    <argument>-o</argument>
                    <argument>${project.build.directory}/generated-sources</argument>
                    <argument>--generate-spring-boot</argument>
                    <argument>--generate-getters</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

The Maven plugin executes the Java code generator tool pkl-tools:

java -cp pkl-tools-0.27.2.jar org.pkl.codegen.java.Main \
src/main/resources/ToolIntegration.pkl \ 
-o ${project.basedir}/src/main --generate-spring-boot

Assuming there’s a Pickle file ToolIntegrationTemplate.pkl, the tool generates the source code corresponding to the Pickle file in the target/generated-sources folder.

The –generate-spring-boot argument includes the necessary Spring Boot class level @ConfigurationProperties annotation into the Java bean. In addition, the –generate-getters argument declares the property keys private.

Luckily for Gradle users, the pkl-gladle plugin offers out-of-box support for generating POJOs.

4.2. Create Configurations in Pkl

Let’s consider a data integration application that fetches data from source systems such as Git and Jira and dispatches them to other target systems.

Let’s begin by defining the Pickle template ToolIntegrationTemplate.pkl to hold the connection properties:

module com.baeldung.spring.pkl.ToolIntegrationProperties

class Connection {
  url:String
  name:String
  credential:Credential
}

class Credential {
  user:String
  password:String
}

gitConnection: Connection
jiraConnection: Connection

Overall, we defined two Pkl classes Connection and Credential, and declared two objects gitConnection and jiraConnection of type Connection.

Next, let’s define gitConnection and jiraConnection in the application.pkl file:

amends "ToolIntegrationTemplate.pkl"
gitConnection {
  url = "https://api.github.com"
  name = "GitHub"
  credential {
    user = "gituser"
    password = read("env:GITHUB_PASSWORD")
  }
}
jiraConnection {
  url = "https://jira.atlassian.com"
  name = "Jira"
  credential {
    user = "jirauser"
    password = read("env:JIRA_PASSWORD")
  }
}

We’ve filled the template with the connection properties of the data sources. Notably, we’ve not hardcoded the passwords in the configuration file. Rather, we use the Pkl read() function to retrieve the passwords from the environment variables, preferably a secure approach.

4.3. Convert Pkl Template to POJO

After defining the configurations in the Pickle file, the POJOs can be generated by executing the Maven goal exec:java defined in the pom.xml file:

mvn exec:java@gen-pkl-java-bind

Eventually, the pkl-tools library generates a POJO and a property file:

Pkl Code Generator

The ToolIntegrationProperties class constitutes two inner classes, Connection and Credential.  It’s also annotated with @ConfigurationProperties which helps bind the properties in application.pkl with the ToolIntegrationProperties object.

4.4. Bind Pickle Configuration to POJO

Let’s first define the service classes JiraService and GitHubService that integrate with the source systems:

public class JiraService {
    private final ToolIntegrationProperties.Connection jiraConnection;

    public JiraService(ToolIntegrationProperties.Connection connection) {
        this.jiraConnection = connection;
    }
   // ...methods getting data from Jira
}
public class GitHubService {
    private final ToolIntegrationProperties.Connection gitConnection;

    public GitHubService(ToolIntegrationProperties.Connection connection) {
        this.gitConnection = connection;
    }
    // ...methods getting data from GitHub
}

Both the services have constructors that take the ToolIntegrationProperties as an argument. Later, the connection details from the argument are assigned to a class-level attribute of type ToolIntegrationProperties.Connection.

Next, we’ll define a configuration class that instantiates the services and declares them as Spring framework beans:

@Configuration
public class ToolConfiguration {
    @Bean
    public GitHubService getGitHubService(ToolIntegrationProperties toolIntegration) {
        return new GitHubService(toolIntegration.getGitConnection());
    }

    @Bean
    public JiraService getJiraService(ToolIntegrationProperties toolIntegration) {
        return new JiraService(toolIntegration.getJiraConnection());
    }
}

During application boot time, the Spring framework binds the method arguments with the configuration from the application.pkl file. Eventually, the beans are instantiated by invoking the service constructors.

4.5. Service Injection and Execution

Finally, we can inject the service beans using the @Autowired annotation in other Spring components.

Using a @SpringBootTest, we’ll verify if the application can load the Jira connection configurations from the Pickle file:

@SpringBootTest
public class SpringPklUnitTest {
    @Autowired
    private JiraService jiraService;

    @Test
    void whenJiraConfigsDefined_thenLoadFromApplicationPklFile() {
        ToolIntegrationProperties.Connection jiraConnection = jiraService.getJiraConnection();
        ToolIntegrationProperties.Credential jiraCredential = jiraConnection.getCredential();

        assertAll(
          () -> assertEquals("Jira", jiraConnection.getName()),
          () -> assertEquals("https://jira.atlassian.com", jiraConnection.getUrl()),
          () -> assertEquals("jirauser", jiraCredential.getUser()),
          () -> assertEquals("jirapassword", jiraCredential.getPassword()),
          () -> assertEquals("Reading issues from Jira URL https://jira.atlassian.com", 
            jiraService.readIssues())
        );
    }
}

For demonstration, we’ve set the Git and JIRA passwords in the environment variables through the pom.xml file. After running the test, we confirm that the Jira connection and credential details match the values defined in the application.pkl file. Finally, we also presume that if implemented correctly, the JiraService#readIssues() executes successfully. For now, the method merely returns a dummy string.

Similarly, let’s check if the application after starting, loads the GitHub connection configurations from the Pickle file:

@SpringBootTest
public class SpringPklUnitTest {
    @Autowired
    private GitHubService gitHubService;

    @Test
    void whenGitHubConfigsDefined_thenLoadFromApplicationPklFile() {
        ToolIntegrationProperties.Connection gitHubConnection = gitHubService.getGitConnection();
        ToolIntegrationProperties.Credential gitHubCredential = gitHubConnection.getCredential();

        assertAll(
          () -> assertEquals("GitHub", gitHubConnection.getName()),
          () -> assertEquals("https://api.github.com", gitHubConnection.getUrl())
          () -> assertEquals("gituser", gitHubCredential.getUser()),
          () -> assertEquals("gitpassword", gitHubCredential.getPassword()),

          () -> assertEquals("Reading issues from GitHub URL https://api.github.com", 
            gitHubService.readIssues())
        );
    }
}

As expected, this time, the GitHub connection and credential details also match the values defined in the application.pkl file. Therefore, with the correct connectivity details, we can presume that the GitHubServiceService#readIssues() if implemented correctly, will connect to GitHub and fetch the issues. Like JiraService#readIssues(), the method only returns a dummy string for now.

5. Conclusion

In this article, we learned the benefits and how to use Pickle files to define the Spring Boot application configuration. However, mastering Pkl language concepts is equally important for designing scalable and maintainable complex configurations. In addition, knowledge of the Spring framework’s feature to bind external properties to POJO is essential.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Microsoft – NPI EA (cat = Spring Boot)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Course – Spring Sale 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

Course – Spring Sale 2025 – NPI (All)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 25% off until 26th May, 2025:

>> EXPLORE ACCESS NOW

Partner – Microsoft – NPI (cat=Spring)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

eBook Jackson – NPI EA – 3 (cat = Jackson)