Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
31 changes: 31 additions & 0 deletions demo/GreetingController.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 15 additions & 0 deletions demo/GreetingService.java
Original file line number Diff line number Diff line change
@@ -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 + "!";
}
}