Lazy post construction

The @AfterInitialized(lazy = true) method is called after the whole dependency tree is resolved, therefore here you can already access each dependency of the service, that was lazily injected.

@Service
class AuthService {
    @Inject(lazy = true)
    private SessionManager sessionManager;
    
    @AfterInitialized(lazy = true)
    private void init() {
        System.out.println(
            "Restored " + sessionManager.getSessions().size() + " sessions."
        );
    }
}

Note that lazily injected fields are guaranteed to exist when the lazy post construction lifecycle is called.

Last updated