> For the complete documentation index, see [llms.txt](https://docs.eventoframework.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.eventoframework.com/evento-framework/eventobundle/injector-and-component.md).

# Injector and @Component

Simplifying Component Management

In the realm of complex applications built with modular components, managing dependencies between them can quickly become a tangled mess. This is where Dependency Injection (DI) shines, promoting loose coupling and fostering code maintainability. The Evento framework embraces DI principles, offering a streamlined approach to handling component lifecycles and dependency resolution.

#### Introducing the Injector: The Dependency Resolver

The injector — a plain `Function<Class<?>, Object>` you supply to the builder via `setInjector` — serves as the heart of Evento's DI system. It takes a class as input and returns a corresponding instance. This instance will have all its required dependencies fulfilled through the injector's magic.

**Behind the Scenes:**

* When Evento encounters a class marked with `@Component` or its derivatives, the framework utilizes the configured injector to create an instance. This instance is managed internally by Evento and is not directly exposed for injection into other components.
* The injector examines the component's constructor, identifying any dependencies declared as parameters.
* The injector then recursively retrieves these dependencies using itself, ensuring a chain reaction of dependency resolution.

**Spring Integration Example:**

The provided code snippet showcases how Evento integrates with Spring Boot's dependency injection:

```java
@Projector(version = 3)
public class DemoProjector {

  private final DemoRepository demoRepository;

  public DemoProjector(DemoRepository demoRepository) {
    this.demoRepository = demoRepository;
  }

  // ...
}
```

In this example, `DemoProjector` is a component requiring a `DemoRepository` instance. During bundle creation:

```java
EventoBundle.Builder.builder()
  .setBasePackage(DemoQueryApplication.class.getPackage())
  .setInjector(factory::getBean) // Spring's getBean function as injector
  .setBundleId(bundleId)
  // 
```

We configure the `setInjector` method with `factory::getBean`. This essentially tells Evento to leverage Spring's `getBean` function to resolve dependencies for the components Evento manages internally. When `DemoProjector` is instantiated, the injector (Spring's `getBean` in this case) will locate and provide the required `DemoRepository` instance.

#### Do not use @Autowired / field injection

Evento instantiates components by reflection and wires their dependencies through the **constructor**, resolved by the configured injector. Field injection annotations (Spring's `@Autowired`, JSR-330's `@Inject`) on component fields are therefore **not** allowed: the framework never processes them, so an annotated field would silently stay `null`.

As of **v2.1.1**, this fails fast — at component registration the framework throws an `IllegalStateException` with a message telling you to switch to constructor injection if any component field is annotated with `@Autowired` / `@Inject`.

Declare the dependency as a constructor parameter instead:

```java
// CORRECT — constructor injection (resolved by the injector)
@Projector(version = 3)
public class DemoProjector {

  private final DemoRepository demoRepository;

  public DemoProjector(DemoRepository demoRepository) {
    this.demoRepository = demoRepository;
  }
}

// WRONG — field injection is rejected at startup (v2.1.1+)
@Projector(version = 3)
public class DemoProjector {

  @Autowired
  private DemoRepository demoRepository; // stays null; throws IllegalStateException
}
```

**Benefits of Evento's DI:**

* **Simplified Component Management:** Evento handles component lifecycles, reducing boilerplate code.
* **Improved Code Maintainability:** Loose coupling through constructor injection promotes testability and modularity.
* **Flexibility:** Supports various injector implementations for integration with different frameworks.

**In Conclusion:**

Evento's DI approach, with its `@Component` annotations and the powerful `Injector` interface, streamlines the process of creating and managing internal components. By leveraging existing frameworks like Spring, Evento simplifies dependency resolution within your application. Remember, the chosen injector depends on your framework or custom implementation.
