JiwonDev

#10 ๋นˆ ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ

by JiwonDev

ํ•ด๋‹น ๊ธ€์€ ๊น€์˜ํ•œ๋‹˜์˜ ์ธํ”„๋Ÿฐ ๊ฐ•์˜๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

# ์Šคํ”„๋ง ๋นˆ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ์ด๋ž€?

์ „์ฒด ๊ณผ์ •์€ ๋‹ค์Œ๊ธ€์— ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ณ  ์šฐ์„  ์ œ์ผ ์˜ค๋ฅธ์ชฝ์— ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ๋งŒ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž.

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

 


# ์Šคํ”„๋ง ๋นˆ ์ƒ๋ช…์ฃผ๊ธฐ ์ฝœ๋ฐฑ ์‹œ์ž‘

๊ฐ„๋‹จํ•˜๊ฒŒ ์™ธ๋ถ€ ๋„คํŠธ์›Œํฌ์—์„œ ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ์ด NetworkClient๋Š” ์•ฑ ์‹œ์ž‘ ์‹œ์ ์— .connect()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์—ฐ๊ฒฐ์„ ๋ฏธ๋ฆฌ ๋งบ์–ด๋‘์–ด์•ผ ํ•˜๊ณ , ์•ฑ์ด ์ข…๋ฃŒ๋˜๋ฉด disConnect()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์—ฐ๊ฒฐ์„ ๋Š์–ด์•ผ ํ•œ๋‹ค.

public class NetworkClient {
    private String url;

    public NetworkClient() {
        System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ url = " + url);
        connect();
        call("์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€");
    }

    public void setUrl(String url) { this.url = url; }
    private void call(String msg) { System.out.println("call = " + url + " msg = " + msg); }
    private void connect() { System.out.println("connect = " + url);}
    public void disconnect() { System.out.println("close = " + url); }

}

์ƒ์„ฑ์ž ๋ถ€๋ถ„์„ ๋ณด๋ฉด url ์ •๋ณด ์—†์ด connect๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋„ˆ๋ฌด๋‚˜ ๋‹น์—ฐํ•œ ๋ง์ด์ง€๋งŒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„์—์„œ๋Š” url์ด ์—†๊ณ , ๋‚˜์ค‘์— ์™ธ๋ถ€์—์„œ setter๋กœ ์ฃผ์ž…ํ•ด์•ผ url์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์Šคํ”„๋ง ๋นˆ๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๊ฐ€์ง„๋‹ค. (์ƒ์„ฑ์ž ์ฃผ์ž…์€ ์ œ์™ธ. ํ˜ธ์ถœ์‹œ ์ฃผ์ž…์ด ํ•จ๊ป˜๋จ)

[ ๊ฐ์ฒด ์ƒ์„ฑ ] ํ›„ -> [ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ]

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

 

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


# ์Šคํ”„๋ง ๋นˆ์˜ ์ด๋ฒคํŠธ ๋ผ์ดํ”„ ์‚ฌ์ดํด

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

 

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

  • ์ธํ„ฐํŽ˜์ด์Šค (InitializingBean, DisposableBean)
  • ์„ค์ • ์ •๋ณด์— ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ, ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ์ง€์ •
  • @PostConstruct, @PreDestory ์–ด๋…ธํ…Œ์ด์…˜

 

@ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ (InitializingBean, DisposableBean)

Spring์—์„œ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ์‹œ์ ์— ํ˜ธ์ถœํ•ด์ค€๋‹ค. ์ฐธ๊ณ ๋กœ ์ด๋Š” ์ดˆ์ฐฝ๊ธฐ์— ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ๋„ˆ๋ฌด ์Šคํ”„๋ง์— ์˜์กด์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์–ด ์ง€๊ธˆ์€ ์ž˜ ์“ฐ์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

public class SimpleClass implements InitializingBean, DisposableBean{
 
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("BEAN ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™” : afterPropertiesSet() ํ˜ธ์ถœ๋จ");
    }
 
    @Override
    public void destroy() throws Exception {
        System.out.println("BEAN ์ƒ์„ฑ ์†Œ๋ฉธ : destroy ํ˜ธ์ถœ๋จ");
    }
 
    // ์ค‘๋žต...
}

 


@ ์„ค์ • ์ •๋ณด์— ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ, ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ

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

// ์–ด๋…ธํ…Œ์ด์…˜
@Bean(initMethod = "init", destroyMethod = "close")

// XML
<bean id="Bean" class="com.spring.bean.BSimpleClass"
                      init-method="init" destroy-method="destroy"/>
@Configuration
static class LifeCycleConfig {
    @Bean(initMethod = "init", destroyMethod = "close")
    public NetworkClient networkClient() {
        NetworkClient networkClient = new NetworkClient();
        networkClient.setUrl("http://hello-spring.dev");
        return networkClient;
    }
}

์ฐธ๊ณ ๋กœ @Bean์˜ destoryMethod ์†์„ฑ์—๋Š” ์•„์ฃผ ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

INFER_METHOD, ์ถ”๋ก  ๋ฉ”์„œ๋“œ (์ข…๋ฃŒ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๋ก ํ•ด์„œ ํ˜ธ์ถœํ•ด์ค€๋‹ค.)

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋Œ€๋ถ€๋ถ„์€ close, shutdown ์ด๋ฆ„์„ ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. @Bean(destoryMethod="...") ๋Š” ๊ธฐ๋ณธ๊ฐ’์ด ์ถ”๋ก ๋ฉ”์„œ๋“œ๋กœ ์ •์˜๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋งˆ์น˜ ๋นˆ์ด ์ž๋™์œผ๋กœ ๋“ฑ๋ก๋˜๋“ฏ์ด ํ•ด๋‹น ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์•„ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•ด์ค€๋‹ค. ์ฆ‰ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ์ข…๋ฃŒ ๋ฉ”์„œ๋“œ๋Š” ์ž˜ ์ž‘๋™ํ•˜๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๋งŒ์•ฝ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์‹ซ๋‹ค๋ฉด destoryMethod="" ์ฒ˜๋Ÿผ ๋นˆ ๊ณต๋ฐฑ์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค. 

 


@PostConstruct, @PreDestory ์–ด๋…ธํ…Œ์ด์…˜

์š”์ฆ˜์€ ๋Œ€๋ถ€๋ถ„ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ณ , ์Šคํ”„๋ง์—์„œ๋„ ๊ถŒ์žฅํ•œ๋‹ค. ์œ„์˜ 2๊ฐ€์ง€๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐฉ์‹์ด ๋‚˜์˜ค๊ธฐ ์ „์— ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ๋ฒ•๋“ค์ด๋‹ค. ์ฐธ๊ณ ๋กœ ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์€ javax.annotation์— ์žˆ๋Š” ๊ฒƒ๋“ค๋กœ ์Šคํ”„๋ง์ด ์•„๋‹Œ ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

 

์œ ์ผํ•œ ๋‹จ์ ์€ ์œ„์—์„œ @Bean(~)์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์€ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, @PostConstruct๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•˜๊ธฐ์— ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ ์šฉํ•˜๊ธฐ ๊นŒ๋‹ค๋กญ๋‹ค.

public class NetworkClient {
    private String url;

    public NetworkClient() {
        System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ, url = " + url);
    }
    
    ... ์ƒ๋žต ...

    @PostConstruct
    public void init() {
        System.out.println("NetworkClient.init");
        connect();
        call("์ดˆ๊ธฐํ™” ์—ฐ๊ฒฐ ๋ฉ”์‹œ์ง€");
    }

    @PreDestroy
    public void close() {
        System.out.println("NetworkClient.close");
        disConnect();
    }
}

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ