From 8ab26011d2c6637ed782f706d3f7641c56e715ea Mon Sep 17 00:00:00 2001 From: Iliyan Velichkov Date: Fri, 19 Jun 2026 16:49:39 +0300 Subject: [PATCH] Add @Component + constructor-injection example and Beans facade Demonstrates the Spring-style DI now supported by the client-Java model: a plain @Component service (GreetingService) injected into a controller (GreetingController) by constructor, plus the Beans facade for programmatic lookup. Existing CountryController is left untouched as the field-injection example. README documents the three styles. Co-Authored-By: Claude Opus 4.8 (1M context) --- README.md | 8 ++++++++ demo/GreetingController.java | 31 +++++++++++++++++++++++++++++++ demo/GreetingService.java | 15 +++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 demo/GreetingController.java create mode 100644 demo/GreetingService.java diff --git a/README.md b/README.md index 5ec1609..5e0ba4e 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,11 @@ controller routes: The OpenAPI fragment for the controller is published automatically at `/services/openapi`. + +## Dependency injection styles + +- **Field injection** — `CountryController` uses `@Inject CountryRepository` (still supported). +- **Constructor injection (preferred)** — `GreetingController` receives a `@Component GreetingService` through its constructor; more testable. +- **Programmatic lookup** — `Beans.get(...)` for when an injection point isn't convenient. + +See the [Develop guide](https://www.dirigible.io/help/develop/dependency-injection/) and the [Java SDK](https://www.dirigible.io/sdk/). diff --git a/demo/GreetingController.java b/demo/GreetingController.java new file mode 100644 index 0000000..59956be --- /dev/null +++ b/demo/GreetingController.java @@ -0,0 +1,31 @@ +package demo; + +import org.eclipse.dirigible.sdk.component.Beans; +import org.eclipse.dirigible.sdk.http.Controller; +import org.eclipse.dirigible.sdk.http.Get; +import org.eclipse.dirigible.sdk.http.PathParam; + +/** + * Constructor injection (preferred, testable). Also shows the Beans facade for programmatic lookup + * when an injection point isn't convenient. + */ +@Controller +public class GreetingController { + + private final GreetingService greetings; + + public GreetingController(GreetingService greetings) { + this.greetings = greetings; + } + + @Get("/greet/{name}") + public String greet(@PathParam("name") String name) { + return greetings.greet(name); + } + + @Get("/greet-via-beans/{name}") + public String greetViaBeans(@PathParam("name") String name) { + return Beans.get(GreetingService.class) + .greet(name); + } +} diff --git a/demo/GreetingService.java b/demo/GreetingService.java new file mode 100644 index 0000000..a94581e --- /dev/null +++ b/demo/GreetingService.java @@ -0,0 +1,15 @@ +package demo; + +import org.eclipse.dirigible.sdk.component.Component; + +/** + * A plain @Component service injected into a controller by constructor — the Spring-style DI the + * client-Java model now supports. + */ +@Component +public class GreetingService { + + public String greet(String name) { + return "Hello, " + name + "!"; + } +}