Java integration
Questi contenuti non sono ancora disponibili nella tua lingua.
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.
Hosting integration
Section titled “Hosting integration”aspire add java --source CommunityToolkitLa CLI Aspire è interattiva; seleziona il risultato corretto quando richiesto:
Select an integration to add:
> java --source CommunityToolkit (CommunityToolkit.Aspire.Hosting.Java)> Other results listed as selectable options...#:package CommunityToolkit.Aspire.Hosting.Java@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.Java" Version="*" />Add Spring Boot app
Section titled “Add Spring Boot app”To add a Spring Boot application to your AppHost, use the AddSpringApp extension method:
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.
Container hosting
Section titled “Container hosting”For production scenarios, host Java applications in containers using JavaAppContainerResourceOptions:
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();Executable hosting
Section titled “Executable hosting”For local development, run Java applications as executables using JavaAppExecutableResourceOptions:
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();Configure endpoints
Section titled “Configure endpoints”Spring Boot applications use the server.port property to configure the port. Use WithHttpEndpoint to expose the endpoint to other resources in the AppHost:
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();Certificate trust (Linux and macOS)
Section titled “Certificate trust (Linux and macOS)”On Linux and macOS, add the following to your Spring Boot application’s application.properties to trust the Aspire development certificates:
server.ssl.trust-store=/path/to/aspire/dev/certificate.p12server.ssl.trust-store-password=p@ssw0rd1For more information, see the Spring Boot SSL documentation.
Observability
Section titled “Observability”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.