Callback lifecycle of Beans in Spring Framework Part2 – LAzy annotation
@Service
@Lazy //changes the moment when the bean instance is created
///will not display the actions in postconstruct and predestroy, the context is created and closed
//PostConstruct and PreDistroy can also be called directly but this is not compliant to Spring standards
//more ofther these methods are marked as private so that they should not be called outside the class
///because is not a business method
public class MyService {
@PostConstruct
public void init(){
System.out.println(“postconstruct”);
}
public void examplebusiness(){
System.out.println(“businessExample”);
}
@PreDestroy
public void destroyy(){
System.out.println(“preDistroy”);
}
}
public class Main {
public static void main(String [] args){
try(
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ProjectConfig.class))
{
System.out.println(“context up”);
MyService ms = ctx.getBean(MyService.class); //get myservice from spring context
ms.examplebusiness();
}
}
}
No Comments