Salta ai contenuti
Docs Try Aspire
Docs Try

Java integration

Questi contenuti non sono ancora disponibili nella tua lingua.

⭐ Community Toolkit Java logo

This article is the reference for the Aspire Java hosting integration from the Aspire Community Toolkit. It enumerates the AppHost APIs you use to model Java applications — including Spring Boot apps — in your AppHost project.

Aspire CLI — Aggiungi pacchetto CommunityToolkit.Aspire.Hosting.Java
aspire add java --source CommunityToolkit

La CLI Aspire è interattiva; seleziona il risultato corretto quando richiesto:

Aspire CLI — Output di esempio
Select an integration to add:
> java --source CommunityToolkit (CommunityToolkit.Aspire.Hosting.Java)
> Other results listed as selectable options...

To add a Spring Boot application to your AppHost, use the AddSpringApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var javaApp = builder.AddSpringApp(
name: "spring-api",
workingDirectory: "../spring-app",
otelAgentPath: "../agents/opentelemetry-javaagent.jar")
.WithHttpEndpoint(port: 8080);
builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(javaApp);
builder.Build().Run();

The AddSpringApp method accepts:

  • name: The resource name shown in the Aspire dashboard.
  • workingDirectory: The path to the directory containing your Spring Boot application.
  • otelAgentPath: The path to the OpenTelemetry Java agent JAR file.

For production scenarios, host Java applications in containers using JavaAppContainerResourceOptions:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var javaApp = builder.AddSpringApp(
name: "spring-api",
new JavaAppContainerResourceOptions
{
OtelAgentPath = "../agents/opentelemetry-javaagent.jar",
ContainerImageName = "my-spring-app:latest",
ContainerRegistry = "myregistry.azurecr.io"
})
.WithHttpEndpoint(port: 8080);
// After adding all resources, run the app...
builder.Build().Run();

For local development, run Java applications as executables using JavaAppExecutableResourceOptions:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var javaApp = builder.AddSpringApp(
name: "spring-api",
new JavaAppExecutableResourceOptions
{
ApplicationName = "spring-app",
OtelAgentPath = "../agents/opentelemetry-javaagent.jar",
WorkingDirectory = "../spring-app"
})
.WithHttpEndpoint(port: 8080);
// After adding all resources, run the app...
builder.Build().Run();

Spring Boot applications use the server.port property to configure the port. Use WithHttpEndpoint to expose the endpoint to other resources in the AppHost:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var javaApp = builder.AddSpringApp(
"spring-api",
"../spring-app",
"../agents/opentelemetry-javaagent.jar")
.WithHttpEndpoint(port: 8080);
// After adding all resources, run the app...
builder.Build().Run();

On Linux and macOS, add the following to your Spring Boot application’s application.properties to trust the Aspire development certificates:

application.properties
server.ssl.trust-store=/path/to/aspire/dev/certificate.p12
server.ssl.trust-store-password=p@ssw0rd1

For more information, see the Spring Boot SSL documentation.

The Java integration uses the OpenTelemetry Java agent to automatically instrument your application and export telemetry to the Aspire dashboard.

Download the OpenTelemetry Java agent and specify its path when calling AddSpringApp. For more information, see OpenTelemetry Java instrumentation.