[Spring] Bean Life Cycle Call Back

2021. 10. 8. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“‹ ๋ชฉ์ฐจ


01. [Spring] ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ
02. [Spring] ์ข‹์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์˜ 5๊ฐ€์ง€ ์›์น™ (SOLID)
03. 
[Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ์Šคํ”„๋ง   
04. [Spring] OCP์™€ DIP ๊ณ ๋ คํ•˜๊ธฐ
05. [Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์›๋ฆฌ ์ ์šฉ - ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ๊ฐœ๋ฐœ
06. 
[Spring] AppConfig ๋ฆฌํŒฉํ„ฐ๋ง    
07. [Spring] Ioc, DI Container  
08. [Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ  
09. [Spring] ์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ  
10. [Spring] Component Scan And Auto wired Start
11. 
[Spring] ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ๋ฒ•    
12. [Spring] Bean Life Cycle Call Back
13. 
[Spring] Bean Scope

 

 

 

๐Ÿ“Œ ๋นˆ ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ


DB ์ปค๋„ฅ์…˜ ํ’€์ด๋‚˜, ๋„คํŠธ์›Œํฌ ์†Œ์ผ“์ฒ˜๋Ÿผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ์‹œ์ ์— ํ•„์š”ํ•œ ์—ฐ๊ฒฐ์„ ๋ฏธ๋ฆฌ ํ•ด๋‘๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ์‹œ์ ์— ์—ฐ๊ฒฐ์„ ๋ชจ๋‘ ์ข…๋ฃŒํ•˜๋Š” ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ ค๋ฉด ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”์™€ ์ข…๋ฃŒ ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ฒƒ์ด์—์š”.

๊ฐ„๋‹จํ•˜๊ฒŒ ์™ธ๋ถ€ ๋„คํŠธ์›Œํฌ์— ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ ๋‹ค๊ณ  ํ•  ๋•Œ, ์‹ค์ œ ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ , ๋‹จ์ˆœํžˆ ๋ฌธ์ž๋งŒ ์ถœ๋ ฅํ•˜๋„๋ก ํ•œ๋ฒˆ ํ•ด ๋ณผ ๊ฒƒ์ด์—์š”.

์ด NetworkClient๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ์‹œ์ ์— connect()๋ฅผ ๋ถˆ๋Ÿฌ๋‚ด์–ด ์—ฐ๊ฒฐ์„ ๋งบ์–ด๋‘๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋˜๋ฉด disCounnect() ๋ถˆ๋Ÿฌ๋‚ด์–ด ์—ฐ๊ฒฐ์„ ๋Š์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

package hello.core.lifecycle;

public class NetworkClient {
 private String url;
 
 public NetworkClient() {
 
 System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = " + url);
 connect();
 call("์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€");
 
 }
 
 public void setUrl(String url) {
 this.url = url;
 }
 
 //์„œ๋น„์Šค ์‹œ์ž‘์‹œ ํ˜ธ์ถœ
 public void connect() {
 System.out.println("connect: " + url);
 }
 
 public void call(String message) {
 System.out.println("call: " + url + " message = " + message);
 }
 
 //์„œ๋น„์Šค ์ข…๋ฃŒ์‹œ ํ˜ธ์ถœ
 public void disconnect() {
 System.out.println("close: " + url);
 }
}

 

์Šคํ”„๋ง ํ™˜๊ฒฝ์„ค์ •๊ณผ ์‹คํ–‰

 

 

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด ์ด์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์ด์—์š”.

 

์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = null
connect: nullcall: null message = ์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€
call: null message = ์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€

 

์ƒ์„ฑ์ž ๋ถ€๋ถ„์„ ๋ณด๋ฉด URL ์ •๋ณด ์—†์ด connect๊ฐ€ ๋ถˆ๋Ÿฌ์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

๋„˜๋‚˜๋„˜๋‚˜ ๋‹น์—ฐํ•˜์ง€๋งŒ, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„์—๋Š” URL์ด ์—†๊ณ , ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์™ธ๋ถ€์—์„œ Setter ์ฃผ์ž…์„ ํ†ตํ•ด setUrl()์ด ๋ถˆ๋Ÿฌ์ ธ์•ผ URL์ด ์ƒ๊ธฐ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

์Šคํ”„๋ง ๋นˆ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์ด์—์š”.

๊ฐ์ฒด ์ƒ์„ฑ => ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…

 

์Šคํ”„๋ง ๋นˆ์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ, ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…์ด ๋‹ค ๋๋‚œ ๋‹ค์Œ์—์•ผ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ค€๋น„๊ฐ€ ์™„๋ฃŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”. ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐํ™” ์ž‘์—…์€ ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…์ด ๋ชจ๋‘ ์™„๋ฃŒ๋˜๊ณ  ๋‚œ ๋’ค์—์•ผ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์ด์—์š”. ๊ทธ๋Ÿฐ๋ฐ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…์ด ๋ชจ๋‘ ์™„๋ฃŒ๋œ ์‹œ์ ์„ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”?

์Šคํ”„๋ง ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…์ด ์™„๋ฃŒ๋˜๋ฉด ์Šคํ”„๋ง ๋นˆ์—๊ฒŒ ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ดˆ๊ธฐํ™” ์‹œ์ ์„ ์•Œ๋ ค์ฃผ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด์—์š”. ๋˜ํ•œ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ง์ „์— ์†Œ๋ฉธ ์ฝœ๋ฐฑ์„ ์ค€๋‹ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•ˆ์ „ํ•˜๊ฒŒ ์ข…๋ฃŒ ์ž‘์—…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

         ๐Ÿ‘‰ ์Šคํ”„๋ง ๋นˆ์˜ ์ด๋ฒคํŠธ ์ƒ๋ช…์ฃผ๊ธฐ

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ => ์Šคํ”„๋ง ๋นˆ ์ƒ์„ฑ => ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž… => ์ดˆ๊ธฐํ™” ์ฝœ๋ฐฑ => ์‚ฌ์šฉ => ์†Œ๋ฉธ์ „ ์ฝœ๋ฐฑ => ์Šคํ”„๋ง ์ข…๋ฃŒ

 

  1. ์ดˆ๊ธฐํ™” ์ฝœ๋ฐฑ : ๋นˆ์ด ์ƒ์„ฑ๋˜๊ณ , ๋นˆ์˜ ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž… ์™„๋ฃŒ ๋’ค ํ˜ธ์ถœ
  2. ์†Œ๋ฉธ์ „ ์ฝœ๋ฐฑ : ๋นˆ์ด ์†Œ๋ฉธ๋˜๊ธฐ ์ง์ „ ํ˜ธ์ถœ

 

์Šคํ”„๋ง์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ์„ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

๐Ÿ’ก ์ฐธ๊ณ  :

๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์ดˆ๊ธฐํ™” ๋ถ„๋ฆฌ

์ƒ์„ฑ์ž๋Š” ํ•„์ˆ˜ ์ •๋ณด(๋งค๊ฐœ ๋ณ€์ˆ˜)๋ฅผ ๋ฐ›๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฑ…์ž„์„ ๊ฐ–๋Š”๋‹ค.
๋ฐ˜๋ฉด์— ์ดˆ๊ธฐํ™”๋Š” ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ๊ฐ’๋“ค์„ ํ™œ์šฉ ์™ธ๋ถ€ ์ปค๋„ฅ์…˜์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋“ฑ ๋ฌด๊ฑฐ์šด ๊ณผ์—…์„ ํ•œ๋‹ค.
๋”ฐ๋ผ์„œ ์ƒ์„ฑ์ž ์•ˆ์—์„œ ๋ฌด๊ฑฐ์šด ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ํ•จ๊ป˜ ํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ์œ ์ง€๋ณด์ˆ˜ ๊ด€์ ์—์„œ ์ข‹๋‹ค.
๋ฌผ๋ก  ์ดˆ๊ธฐํ™” ์ž‘์—…์ด ๋‚ด๋ถ€ ๊ฐ’๋“ค๋งŒ ์•ฝ๊ฐ„ ๋ณ€๊ฒฝํ•˜๋Š” ์ •๋„๋กœ ๋‹จ์ˆœํ•œ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž์—์„œ ํ•œ๋ฒˆ์— ๋‹ค ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์‹ฑ๊ธ€ํ†ค ๋นˆ๋“ค์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ, ์‹ฑ๊ธ€ํ†ค ๋นˆ๋“ค๋„ ํ•จ๊ป˜ ์ข…๋ฃŒ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ง์ „์— ์†Œ๋ฉธ์ „ ์ฝœ๋ฐฑ์ด ๋ฐœ์ƒ. ์‹ฑ๊ธ€ํ†ค์ฒ˜๋Ÿผ ์ปจํ…Œ์ด๋„ˆ์˜ ์‹œ์ž‘๊ณผ ์ข…๋ฃŒ๊นŒ์ง€ ์ƒ์ข…ํ•˜๋Š” ๋นˆ๋„ ์žˆ์œผ๋‚˜, ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ์งฆ์€ ๋นˆ๋“ค๋„ ์žˆ๋Š”๋ฐ, ์ด ๋นˆ๋“ค์€ ์ปจํ…Œ์ด๋„ˆ์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ํ•ด๋‹น ๋นˆ์ด ์ข…๋ฃŒ๋˜๊ธฐ ์ง์ „์— ์†Œ๋ฉธ์ „ ์ฝœ๋ฐฑ์ด ์ผ์–ด๋‚œ๋‹ค.

 

์Šคํ”„๋ง์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋นˆ ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ์„ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

  1. ์ธํ„ฐํŽ˜์ด์Šค(InitalizingBean, DisposableBean)
  2. ์„ค์ • ์ •๋ณด์— ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ, ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ ์ง€์ •
  3. @PostConstruct, @PreDestroy ์ง€์›

 

 

    ๐Ÿ“ ์ธํ„ฐํŽ˜์ด์Šค InitializingBean, DisposableBean

 

package hello.core.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class NetworkClient implements InitializingBean, DisposableBean {
 private String url;
 public NetworkClient() {
 System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = " + url);
 }
 
 public void setUrl(String url) {
 this.url = url;
 }
 
 //์„œ๋น„์Šค ์‹œ์ž‘์‹œ ํ˜ธ์ถœ
 public void connect() {
 System.out.println("connect: " + url);
 }
 
 public void call(String message) {
 System.out.println("call: " + url + " message = " + message);
 } //์„œ๋น„์Šค ์ข…๋ฃŒ์‹œ ํ˜ธ์ถœ
 
 public void disConnect() {
 System.out.println("close + " + url);
 }
 
 @Override
 public void afterPropertiesSet() throws Exception {
 connect();
 call("์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€");
 }
 
 @Override
 public void destroy() throws Exception {
 disConnect();
 }
}

 

InitializingBean์€ afterPropertiesSet()์„ ํ†ตํ•ด ์ดˆ๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด์—์š”.
DisponsableBean์€ destroy()๋ฅผ ํ†ตํ•ด ์†Œ๋ฉธ์„ ์ง€์›ํ•œ๋‹ต๋‹ˆ๋‹ค.

 

๐Ÿ’ป ์ถœ๋ ฅ ๊ฒฐ๊ณผ

์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = null
NetworkClient.afterPropertiesSet
connect: http://hello-spring.dev
call: http://hello-spring.dev message = ์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€
13:24:49.043 [main] DEBUG 
org.springframework.context.annotation.AnnotationConfigApplicationContext - 
Closing NetworkClient.destroy
close + http://hello-spring.dev

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ๊ฐ€ ์ฃผ์ž… ์™„๋ฃŒ ๋’ค ์ ์ ˆํ•˜๊ฒŒ ๋ถˆ๋ ค์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋ฆฌ๊ณ , ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ข…๊ต๊ฐ€ ํ˜ธ์ถœ๋˜์ž ๋งˆ์ž ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ ๊ฒƒ๋„ ๋ณด์ด๋„ค์š”!

 

         ๐Ÿ‘‰ ์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ์ธํ„ฐํŽ˜์ด์Šค ๋‹จ์ 

์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์Šคํ”„๋ง ์ „์šฉ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์Šคํ”„๋ง ์ „์šฉ ์ธํ„ฐํŽ˜์ด์Šค์— ์˜์กดํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ณ , ๋‚ด๊ฐ€ ์ฝ”๋“œ๋ฅผ ๊ณ ์น  ์ˆ˜ ์—†๋Š” ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

๐Ÿ’ก ์ฐธ๊ณ  :

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ดˆ๊ธฐํ™”, ์ข…๋ฃŒ ๋ฐฉ๋ฒ•์€ ์Šคํ”„๋ง ์ดˆ์ฐฝ๊ธฐ์— ๋‚˜์˜จ ๋ฐฉ๋ฒ•์ด๋ฉฐ, ์ง€๊ธˆ์€ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌ

 

๋ฐ˜์‘ํ˜•

 

    ๐Ÿ“ ๋นˆ ๋“ฑ๋ก ์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ ์ง€์ •

์„ค์ • ์ •๋ณด์— @Bean(initMethod = "init", destroyMethod = "close") ์ฒ˜๋Ÿผ ์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ ์ง€์ •์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด์—์š”.

 

package hello.core.lifecycle;
public class NetworkClient {
 private String url;
 public NetworkClient() {
 System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = " + url);
 }
 
 public void setUrl(String url) {
 this.url = url;
 }
 
 //์„œ๋น„์Šค ์‹œ์ž‘์‹œ ํ˜ธ์ถœ
 public void connect() {
 System.out.println("connect: " + url);
 }
 
 public void call(String message) {
 System.out.println("call: " + url + " message = " + message); 
 }
 
 //์„œ๋น„์Šค ์ข…๋ฃŒ์‹œ ํ˜ธ์ถœ
 public void disConnect() {
 System.out.println("close + " + url);
 }
 
 public void init() {
 System.out.println("NetworkClient.init");
 connect();
 call("์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€");
 }
 
 public void close() {
 System.out.println("NetworkClient.close");
 disConnect();
 }
}

 

 

์„ค์ • ์ •๋ณด์— ์ดˆ๊ธฐํ™” ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ ์ง€์ •

@Configuration static class LifeCycleConfig {
 @Bean(initMethod = "init", destroyMethod = "close")
 public NetworkClient networkClient() {
 	NetworkClient networkClient = new NetworkClient();
 	networkClient.setUrl("http://hello-spring.dev");
 	return networkClient;
 }
}

 

 

๐Ÿ’ป ์ถœ๋ ฅ ๊ฒฐ๊ณผ

์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = nullNetworkClient.init
connect: http://hello-spring.dev
call: http://hello-spring.dev message = ์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€
13:33:10.029 [main] DEBUG 
org.springframework.context.annotation.AnnotationConfigApplicationContext - 
Closing NetworkClient.close
close + http://hello-spring.dev

 

์„ค์ • ์ •๋ณด ์‚ฌ์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•์ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

  1. ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ž์œ ๋กญ๊ฒŒ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  2. ์Šคํ”„๋ง ๋นˆ์ด ์Šคํ”„๋ง ์ฝ”๋“œ์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค.
  3. ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ์„ค์ • ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ๊ณ ์น  ์ˆ˜ ์—†๋Š” ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋„ ์ดˆ๊ธฐํ™”, ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ข…๋ฃŒ ๋ฉ”์„œ๋“œ ์ถ”๋ก 

  1. @Bean์˜ destroyMethod ์†์„ฑ์— ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ ์กด์žฌ
  2. Library๋Š” ๋Œ€๋ถ€๋ถ„ close, shutdown ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  3. @Bean์˜ destroyMethod๋Š” ๊ธฐ๋ณธ๊ฐ’์ด (inferred - ์ถ”๋ก )์œผ๋กœ ๋“ฑ๋ก
  4. ์ด ์ถ”๋ก  ๊ธฐ๋Šฅ์€ close, shutdown ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉฐ, ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๋ก ํ•ด์„œ ๋ถˆ๋Ÿฌ์ค€๋‹ค.
  5. ์œ„์™€ ๊ฐ™์€ ์ด์œ ๋กœ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋ฉด ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ๋Š” ๋”ฐ๋กœ ๊ธฐ์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  6. ์ถ”๋ก  ๊ธฐ๋Šฅ์„ Disable ํ•˜๋ ค๋ฉด destroyMethod="" ์ฒ˜๋Ÿผ ๋นˆ ๊ณต๋ฐฑ ์ง€์ • 

 

 

    ๐Ÿ“ @PostConstruct, @PreDestroy

 

NetworkClient.java

 

BeanLifeCycleTest.java

 

 

๐Ÿ’ป ์ถœ๋ ฅ ๊ฒฐ๊ณผ

์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = null
NetworkClient.init
connect: http://hello-spring.dev
call: http://hello-spring.dev message = ์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€
19:40:50.269 [main] DEBUG 
org.springframework.context.annotation.AnnotationConfigApplicationContext - 
Closing NetworkClient.close
close + http://hello-spring.dev

 

@PostConstruct, @PreDestroy ์ด๊ฒƒ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€์žฅ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ดˆ๊ธฐํ™”์™€ ์ข…๋ฃŒ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

@PostConstruct, @PreDestroy ํŠน์ง•

์ตœ์‹  ์Šคํ”„๋ง์—์„œ ๊ฐ€์žฅ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ณ , ์–ด๋…ธํ…Œ์ด์…˜ ํ•˜๋‚˜๋งŒ ๋ถ™์ด๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊น”๋”ํ•˜๊ณ , ๊ฐ„ํŽธํ•œ ๊ฒƒ์ด์—์š”.

ํŒจํ‚ค์ง€๋ฅผ ์ž˜ ๋ณด๋ฉด javax.annotation.PostConstruct์ธ๋ฐ, ์Šคํ”„๋ง์— ์ข…์†์ด ์•„๋‹Œ JSR-250์ด๋ผ๋Š” JAVA Standard์ธ ๊ฒƒ์ด์—์š”. ๋•Œ๋ฌธ์— ์Šคํ”„๋ง์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์ž˜ ์–ด์šธ๋ฆฌ๋Š” ์นœ๊ตฌ์ด์ง€๋งŒ, ์œ ์ผํ•œ ๋‹จ์ ์œผ๋กœ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ์ ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์ด์—์š”. ๋•Œ๋ฌธ์— ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ดˆ๊ธฐํ™”, ์ข…๋ฃŒ ์‹œ์—๋Š” @Bean์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

์ •๋ฆฌํ•˜์ž๋ฉด

@PostConstruct, @PreDestroy๋ฅผ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค!

์ฝ”๋“œ๋ฅผ ๊ณ ์น  ์ˆ˜ ์—†๋Š” ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™”, ์ข…๋ฃŒํ•ด์•ผ ํ•œ๋‹ค๋ฉด @Bean์˜ initMethod, destroyMethod๋ฅผ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค!

 

 

 


 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€ ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ช…๋ก์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

'Back-End ์ž‘์—…์‹ค > Spring Framework' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Spring] @Builder (Builder Patter)  (0) 2021.10.11
[Spring] Bean Scope  (0) 2021.10.10
[Spring] ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ๋ฒ•  (0) 2021.10.07
[Spring] Component Scan And Auto wired Start  (0) 2021.10.06
[JPQL] left ( outer ) join  (0) 2021.10.05