Spring. @PostConstruct and @PreDestroy

 package com.example.demo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
//@Scope("prototype")
public class TennisCoach implements Coach {

@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;

public TennisCoach() {
System.out.println("TennisCoach: inside default constructor");
}

@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}

@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}

@PostConstruct
public void doMyStartupStuff() {
System.out.println("Inside Post Construct method");
}

@PreDestroy
public void doMyCleanUpStuff() {
System.out.println("Inside Pre Destroy method");
}
}

* For "prototype" scoped beans, Spring does not call the @PreDestroy method.

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class