Flattened bundle. Content from source markdown guides is inlined below.
How to create and annotate beans with avaje-inject.
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.
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");
}
}Control bean lifecycle:
@Singleton // One instance (default)
public class Single { }
@Prototype // New instance each time
public class Multi { }- Learn dependency injection
- Use factory methods