// SomeDependency.java (An independent class) @Component public class SomeDependency { public void getReady() { System.out.println("Some logic using SomeDependency"); } } // SomeClass.java (A dependent class) @Component public class SomeClass { private SomeDependency someDependency; public SomeClass(SomeDependency someDependency) { super(); this.someDependency = someDependency; System.out.println("All dependencies are ready!"); // This will be printed during bean initialization and dependency injection } @PostConstruct // This method will be automatically invoked immediately after all the dependency injection is completed public void initialize() { someDependency.getReady(); } @PreDestroy // This method will be automatically invoked immediately before the bean is removed or goes out of scope public void cleanup() { System.out.println("Cleanup"); } } // PrePostAnnotationsContextLauncherApplication.java (The application launcher) @Configuration @ComponentScan public class PrePostAnnotationsContextLauncherApplication { public static void main(String[] args) { try (var context = new AnnotationConfigApplicationContext(PrePostAnnotationsContextLauncherApplication.class)) { Arrays.stream(context.getBeanDefinitionNames()) .forEach(System.out::println); } } }