Spring. Defining init and destroy methods.

1. Access modifier

    The method can have any access modifier (public, protected, private).

2. Return type

    The method can have any return type. However, "void" is most commonly used. If you give a return type just note that you will not be able to capture the return value. As a result, "void" is commonly used. 

 3. Method name

    The method can have any method name.

4. Arguments

    The method can not accept any arguments. The method should be no-arg.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="myFortuneService" class="com.example.demo.HappyFortuneService">
</bean>

<bean id = "myCoach" class="com.example.demo.TrackCoach" init-method="doMyStartupStuff" destroy-method="doMyCleanupStuffYoYo">
<constructor-arg ref="myFortuneService"/>
</bean>


</beans>





package com.example.demo;

public class TrackCoach implements Coach {

private FortuneService fortuneService;

public TrackCoach(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}

@Override
public String getDailyWorkout() {
return "Run a hard 5k";
}

@Override
public String getDailyFortune() {
return "Just do it: " + fortuneService.getFortune();
}

// add an init method
public void doMyStartupStuff() {
System.out.println("TrackCoack: inside method doMyStartupStuff");
}

// add a destroy method
public void doMyCleanupStuffYoYo() {
System.out.println("TrackCoack: inside method doMyCleanupStuffYoYo");
}
}

Комментарии

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

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class