๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] ๋นˆ์˜ Scope - ์‹ฑ๊ธ€ํ†ค๊ณผ ํ”„๋กœํ† ํƒ€์ž…

by Leica 2020. 3. 3.
๋ฐ˜์‘ํ˜•

[Spring] ๋นˆ์˜ Scope - ์‹ฑ๊ธ€ํ†ค๊ณผ ํ”„๋กœํ† ํƒ€์ž…

๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ ์•„๋ฌด๋Ÿฐ ์„ค์ •์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋นˆ์€ ์‹ฑ๊ธ€ํ†ค scope์„ ๊ฐ–๋Š”๋‹ค.

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

 

1. Singleton Scope

Single, Proto ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๊ณ  @Component๋ฅผ ๋ถ™์—ฌ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

 

Single.java

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class Single {
 
    @Autowired
    private Proto proto;
 
    public Proto getProto() {
        return proto;
    }
}
cs

 

Proto.java

1
2
3
4
5
import org.springframework.stereotype.Component;
 
@Component
public class Proto {
}
cs

Single์— Proto๋ฅผ ์ฃผ์ž…ํ•œ๋‹ค.

 

AppRunner.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
@Component
public class AppRunner implements ApplicationRunner {
 
    @Autowired
    Single single;
 
    @Autowired
    Proto proto;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(proto);
        System.out.println(single.getProto());
    }
}
cs

ApplicationRunner๋ฅผ ๋งŒ๋“ค๊ณ  Single๊ณผ Proto๋ฅผ ์ฃผ์ž…๋ฐ›์•„ runner๊ฐ€ ์ฃผ์ž…๋ฐ›์€ Proto์™€ Single์—์„œ ๊ฐ€์ ธ์˜จ Proto๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์ž.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋กœ ๋‘ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ํ•˜๋‚˜์˜ ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค์ธ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Proto๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ• ๋•Œ scope์„ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์‹ฑ๊ธ€ํ†ค scope์„ ๊ฐ–๊ณ , ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘ํ• ๋•Œ ์ƒ์„ฑ๋˜๋Š” ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์“ฐ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

1) ์‹ฑ๊ธ€ํ†ค ๋นˆ ์‚ฌ์šฉ์‹œ ์ฃผ์˜ํ•  ์ 

- ํ”„๋กœํผํ‹ฐ ๊ณต์œ 

์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋Š” thread-safeํ•˜๋‹ค๊ณ  ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค.

๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋Š” ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ์— ์˜ํ•ด ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š”๋ฐ

๊ฐ€๋ น ์“ฐ๋ ˆ๋“œ A์—์„œ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ x๋กœ ๋ฐ”๊พธ๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๊ณผ์ •์—์„œ ์“ฐ๋ ˆ๋“œ B๊ฐ€ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ y๋กœ ๋ฐ”๊พธ๋ฉด ์“ฐ๋ ˆ๋“œ A ์ž…์žฅ์—์„œ๋Š” ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ์ˆ˜๋„ ์žˆ๋Š”๊ฒƒ์ด๋‹ค.

 

- Application ์ดˆ๊ธฐ ๊ตฌ๋™ ์‹œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

์‹ฑ๊ธ€ํ†ค ๋นˆ์€ ๋ชจ๋‘ ๊ธฐ๋ณธ์ ์œผ๋กœ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ๋™ ์‹œ ์ƒ์„ฑ๋˜๋ฏ€๋กœ ์‹ฑ๊ธ€ํ†ค ๋นˆ์ด ๋งŽ์„ ์ˆ˜๋ก ๊ตฌ๋™ ์‹œ๊ฐ„์ด ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2. Prototype Scope

๋นˆ์˜ scope ์„ค์ •์€ @Scope ์• ๋…ธํ…Œ์ด์…˜์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœํ† ํƒ€์ž… scope์œผ๋กœ ์„ค์ •ํ•˜๋ ค๋ฉด @Scope("prototype")๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.

 

Proto.java

1
2
3
4
5
6
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
@Component @Scope("prototype")
public class Proto {
}
cs

Proto๋งŒ @Scope๋ฅผ ๋ถ™์—ฌ scope๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

 

AppRunner.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class AppRunner implements ApplicationRunner {
 
    @Autowired
    ApplicationContext ctx;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("Proto:");
        System.out.println(ctx.getBean(Proto.class));
        System.out.println(ctx.getBean(Proto.class));
        System.out.println(ctx.getBean(Proto.class));
 
        System.out.println("Single:");
        System.out.println(ctx.getBean(Single.class));
        System.out.println(ctx.getBean(Single.class));
        System.out.println(ctx.getBean(Single.class));
    }
}
cs

Runner์—์„œ ApplicationContext๋ฅผ ์ฃผ์ž…๋ฐ›๊ณ  Proto์™€ Single ๋นˆ์„ ์„ธ๋ฒˆ์”ฉ ์ถœ๋ ฅํ•ด๋ณด์ž.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

Proto๋Š” ๋ชจ๋‘ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค์ด๊ณ  Single์€ ๋ชจ๋‘ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

 

ํ”„๋กœํ† ํƒ€์ž… scope๋Š” ์‹ฑ๊ธ€ํ†ค scope๊ณผ ๋‹ฌ๋ฆฌ IoC์—์„œ ๋นˆ์„ ๋ฐ›์•„์˜ฌ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋นˆ์˜ scope๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ด€๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด spring์˜ ์žฅ์ ์ด๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ํ”„๋กœํ† ํƒ€์ž… ๋นˆ๊ณผ ์‹ฑ๊ธ€ํ†ค ๋นˆ์„ ์„ž์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3. ํ”„๋กœํ† ํƒ€์ž… ๋นˆ๊ณผ ์‹ฑ๊ธ€ํ†ค ๋นˆ์„ ์„ž์–ด์„œ ์‚ฌ์šฉ

1) ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์—์„œ ์‹ฑ๊ธ€ํ†ค ๋นˆ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ

์ด ๊ฒฝ์šฐ๋Š” ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋กœํ† ํƒ€์ž… ๋นˆ Proto์—์„œ ์‹ฑ๊ธ€ํ†ค ๋นˆ Single์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

 

1
2
3
4
5
6
7
8
9
10
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
@Component @Scope("prototype")
public class Proto {
    
    @Autowired
    Single single;
}
cs

 

ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์˜ ์ธ์Šคํ„ด์Šค๋Š” ๊ณ„์† ์ƒ์„ฑ๋˜๊ณ  ์ฃผ์ž…๋ฐ›๋Š” ์‹ฑ๊ธ€ํ†ค ๋นˆ์€ ๊ณ„์† ๋™์ผํ•œ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค์ผ๊ฒƒ์ด๋‹ค.

์ด๋Ÿฌํ•œ ๊ฒฐ๊ณผ๋Š” ๊ฐœ๋ฐœ์ž์˜ ๊ธฐ๋Œ€, ์˜๋„์—์„œ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

 

2) ์‹ฑ๊ธ€ํ†ค ๋นˆ์—์„œ ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ

์ด ๊ฒฝ์šฐ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹ฑ๊ธ€ํ†ค ๋นˆ์˜ ์ธ์Šคํ„ด์Šค๋Š” ๋‹จ ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ๋•Œ ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์˜ ์ฃผ์ž…๋„ ์ด๋ฏธ ์™„๋ฃŒ๋œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์‹ฑ๊ธ€ํ†ค ๋นˆ์„ ์‚ฌ์šฉํ• ๋•Œ ์ฃผ์ž…๋ฐ›์€ ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์ด ๋ณ€๊ฒฝ(์—…๋ฐ์ดํŠธ) ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

AppRunner.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class AppRunner implements ApplicationRunner {
 
    @Autowired
    ApplicationContext ctx;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("Proto:");
        System.out.println(ctx.getBean(Proto.class));
        System.out.println(ctx.getBean(Proto.class));
        System.out.println(ctx.getBean(Proto.class));
 
        System.out.println("Single:");
        System.out.println(ctx.getBean(Single.class));
        System.out.println(ctx.getBean(Single.class));
        System.out.println(ctx.getBean(Single.class));
 
        System.out.println("Proto by Single:");
        System.out.println(ctx.getBean(Single.class).getProto());
        System.out.println(ctx.getBean(Single.class).getProto());
        System.out.println(ctx.getBean(Single.class).getProto());
    }
}
cs

์‹ฑ๊ธ€ํ†ค ๋นˆ์ธ Single์„ ํ†ตํ•ด ํ”„๋กœํ† ํƒ€์ž… ๋นˆ Proto๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ถœ๋ ฅํ•ด๋ณด์ž.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

์œ„์™€ ๊ฐ™์ด ์‹ฑ๊ธ€ํ†ค ๋นˆ Single ์•ˆ์˜ Proto๋Š” ํ”„๋กœํ† ํƒ€์ž…์ธ๋ฐ๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ฃผ์ž…๋˜๊ณ ๋‚˜์„œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒˆ๋กœ ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค.

์ด๋ ‡๊ฒŒ ์‹ฑ๊ธ€ํ†ค ๋นˆ์—์„œ ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ์ฐธ์กฐํ• ๋•Œ๋Š” ๋‹ค์Œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์Šคํ„ด์Šค๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

4. ์—…๋ฐ์ดํŠธ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1) ๋ฐฉ๋ฒ• 1 - proxyMode ์„ค์ •

ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์˜ @Scope ์• ๋…ธํ…Œ์ด์…˜์— proxyMode ์†์„ฑ์„ ์„ค์ •ํ•ด์ค€๋‹ค.

 

Proto.java

1
2
3
4
5
6
7
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
 
@Component @Scope(scopeName = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Proto {
}
cs

proxyMode ์†์„ฑ์€ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ scopedProxyMode.DEFAULT์ด๋‹ค.

DEFAULT๋Š” proxy๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜ต์…˜์ด๊ณ  proxy๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ TARGET_CLASS, ์ธํ„ฐํŽ˜์ด์Šค์ผ ๊ฒฝ์šฐ INTERFACES๋กœ ์„ค์ •ํ•œ๋‹ค.

 

  • scopedProxyMode.DEFAULT : Proxy๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
  • scopedProxyMode.TARGET_CLASS : Proxy๋ฅผ ์‚ฌ์šฉํ•จ(ํด๋ž˜์Šค)
  • scopedProxyMode.INTERFACES : Proxy๋ฅผ ์‚ฌ์šฉํ•จ(์ธํ„ฐํŽ˜์ด์Šค)

์‹คํ–‰ ๊ฒฐ๊ณผ

 

Proxy๋ฅผ ์“ด๋‹ค๋Š” ๊ฒƒ์€ ๋Œ€์ƒ ๋นˆ์„ proxy๋กœ ๊ฐ์‹ผ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ํ•ด๋‹น ๋นˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ๋“ค์ด ํ”„๋ก์‹œ๋กœ ๊ฐ์‹ผ ๋นˆ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•œ๋‹ค.

 

์‹ฑ๊ธ€ํ†ค ๋นˆ์ด ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ์‚ฌ์šฉํ• ๋•Œ proxy๋กœ ๊ฐ์‹ธ์•ผํ•˜๋Š” ์ด์œ ๋Š”, ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ์ง์ ‘ ์ฐธ์กฐํ•˜๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์ค„ ์—ฌ์ง€๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ฆ‰ ๋งค๋ฒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ํ”„๋ก์‹œ๋กœ ๊ฐ์‹ธ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

์ฐธ๊ณ ๋กœ ์›๋ž˜ JDK ์•ˆ์— ์žˆ๋Š” dynamic proxy๋Š” ์ธํ„ฐํŽ˜์ด์Šค์˜ ํ”„๋ก์‹œ๋งŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค์˜ ํ”„๋ก์‹œ๋Š” ์จ๋“œ ํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ์™€ ๊ฐ™์ด ScopedProxyMode.TARGET_CLASS๋ผ๊ณ  ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ํƒ€๊ฒŸ ํด๋ž˜์Šค์˜ proxy๋ฅผ ๋งŒ๋“ค๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ ์‹ค์ œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ์‹ธ๋Š” ํ”„๋ก์‹œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์ด ํ”„๋ก์‹œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์‹ค์งˆ์ ์œผ๋กœ ์ฃผ์ž…๋˜๋Š” ๋นˆ์€ ํ”„๋ก์‹œ ๋นˆ์ด๋‹ค.

ํ”„๋ก์‹œ ๋นˆ๋„ ์›๋ณธ ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ์ƒ์†ํ•ด์„œ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์€ ๋™์ผํ•˜๋‹ค.

ํƒ€์ž…์ด ๊ฐ™์œผ๋ฏ€๋กœ ๋ฌธ์ œ์—†์ด ์ฃผ์ž…๋  ์ˆ˜ ์žˆ๋‹ค.

 

2) ๋ฐฉ๋ฒ• 2 - ObjectProvider ์‚ฌ์šฉ

์ด ๋ฐฉ๋ฒ•์€ ๋นˆ ์„ค์ •์ด ์•„๋‹Œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์„œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์œ„์—์„œ ์„ค์ •ํ•œ proxyMode๋ฅผ ์›๋ณตํ•˜๊ณ  Single.java๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

 

Single.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class Single {
 
    @Autowired
    private ObjectProvider<Proto> proto;
 
    public Proto getProto() {
        return proto.getIfAvailable();
    }
}
cs

 

ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ์ฃผ์ž…๋ฐ›๋Š” ํ•„๋“œ์˜ ํƒ€์ž…์„ ObjectProvider๋กœ ๊ฐ์‹ธ๊ณ  getter์—์„œ getIfAvailable()๋กœ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•œ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์ด ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ ์ˆ˜์ •์ด ํ•„์š”ํ•˜๋ฉฐ spring ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฐ” ๊ฐ์ฒด๊ฐ€ POJO ์Šคํƒ€์ผ์—์„œ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋“ , ์ด๋ ‡๊ฒŒ Scope์ด ๋„“์€ ๋นˆ(์‹ฑ๊ธ€ํ†ค ๋นˆ)์—์„œ scope์ด ์งง์€ ๋นˆ(ํ”„๋กœํ† ํƒ€์ž… ๋นˆ)์„ ์ฃผ์ž…๋ฐ›์„ ๋•Œ๋Š” ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ์ ์„ ๋ฐ˜๋“œ์‹œ ์•Œ์•„๋‘์ž.

 

References

์ธํ”„๋Ÿฐ - ๋ฐฑ๊ธฐ์„ ๋‹˜์˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ํ•ต์‹ฌ ๊ธฐ์ˆ 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€