Skip to content

Latest commit

 

History

History
63 lines (43 loc) · 1.11 KB

File metadata and controls

63 lines (43 loc) · 1.11 KB

Avaje Bundle — Setup (Flattened)

Flattened bundle. Content from source markdown guides is inlined below.


Source: inject/creating-beans.md

Creating Beans with Avaje Inject

How to create and annotate beans with avaje-inject.

Basic Bean

Mark a class as a singleton bean:

import jakarta.inject.Singleton;

@Singleton
public class UserService {
  public User findById(long id) {
    return new User(id, "John");
  }
}

The @Singleton annotation makes it a singleton - one instance for the application.

Implementing Interfaces

Beans can implement interfaces if desired:

public interface UserService {
  User findById(long id);
}

@Singleton
public class UserServiceImpl implements UserService {
  @Override
  public User findById(long id) {
    return new User(id, "John");
  }
}

Scopes

Control bean lifecycle:

@Singleton          // One instance (default)
public class Single { }

@Prototype          // New instance each time
public class Multi { }

Next Steps