Edit

Share via


.NET Aspire Java/Spring hosting integration

Includes: Hosting integration included Hosting integration only — Client integration not included Client integration not included

Note

This integration is part of the .NET Aspire Community Toolkit and isn't officially supported by the .NET Aspire team.

In this article, you learn how to use the .NET Aspire Java/Spring hosting integration to host Java/Spring applications using either the Java runtime or a container.

Prerequisites

This integration requires the OpenTelemetry Agent for Java to be downloaded and placed in the agents directory in the root of the project. Depending on your preferred shell, use either of the following commands to download the agent:

# bash/zsh
mkdir -p ./agents
wget -P ./agents \
    https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar

Linux and macOS certificate trust

On Linux and macOS platforms, you might need to import the .NET Aspire OpenTelemetry certificate into the Java certificate store for telemetry to work properly. Without this step, your Java application will start successfully, but telemetry collection might fail with certificate errors.

To add the certificate to the Java truststore, you can use the following steps:

  1. Export the .NET Aspire dashboard certificate (this varies by your setup)
  2. Import it into the Java truststore using the keytool command:
keytool -import -trustcacerts -alias aspire-dashboard \
    -file aspire-dashboard.crt \
    -keystore $JAVA_HOME/lib/security/cacerts \
    -storepass changeit

Note

The exact steps for obtaining and importing the certificate may vary depending on your development environment and .NET Aspire configuration.

Get started

To get started with the .NET Aspire Java/Spring hosting integration, install the 📦 CommunityToolkit.Aspire.Hosting.Java NuGet package in the AppHost project.

dotnet add package CommunityToolkit.Aspire.Hosting.Java

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example Usage

The following sections detail various example usage scenarios, from hosting a containerized Spring app to hosting an executable Spring app.

In the Program.cs file of your app host project, call the AddSpringApp method to define the containerized Spring app. The JavaAppContainerResourceOptions allows you to specify the container image and OpenTelemetry agent configuration.

var containerapp = builder.AddSpringApp(
    "containerapp",
    new JavaAppContainerResourceOptions
    {
        ContainerImageName = "your-registry/your-spring-app:latest",
        OtelAgentPath = "./agents"
    });

The ContainerImageName should point to your Spring Boot application's container image, and OtelAgentPath specifies the path within the container where the OpenTelemetry Java agent is located.

See also