<์ถœ์ฒ˜ ์ธํ”„๋Ÿฐ>



8์›” ์ „์— ๋‹ค ๋๋‚ด๊ธฐ๋กœ ํ•œ ๊ณ„ํš,,,! ๊ฐ•์˜๋Š” ๋ชจ๋‘ ๋“ค์—ˆ์ง€๋งŒ ์™œ์ธ์ง€ ๋ธ”๋กœ๊ทธ ์ž‘์„ฑ์€ ๋ฏธ๋ค„์กŒ๋‹ค. ๊ทธ๋ž˜๋„ ์˜์ƒ ๋‹ค ๋“ค์€๊ฒŒ ์–ด๋””๋ƒ๋ฉฐ ์•ฝ๊ฐ„์˜ ์œ„๋กœ์™€ ํ•จ๊ป˜ ๋ธ”๋กœ๊ทธ ์ž‘์„ฑ์„ ํ•ด๋ณด๋ คํ•œ๋‹ค. ์ฝ”๋กœ๋‚˜๋กœ ์ธํ•ด ์ฒด๋ ฅ์ด ๋ฐ•์‚ด๋‚œ๊ฑด์ง€, ๊ทธ๋ƒฅ ์ด๋ฏธ ๋ฐ•์‚ด๋‚œ๊ฑธ ๋‚ด๊ฐ€ ๋ชฐ๋ž๋˜ ๊ฑด์ง€ ์•Œ ์ˆ˜ ์—†์ง€๋งŒ ์œ„๊ฒฝ๋ จ ํ„ฐ์ง€๋ฉฐ ๊ธธ๋ฐ”๋‹ฅ์— ์ฃผ์ €์•‰๋Š” ์ธ์ƒ์ด์–ด๋„ ์˜ค๋Š˜๋„ ํ™”์ดํŒ…!! ๋ชจ๋‘๋“ค ํ™”์ดํŒ…!



 

๐Ÿงธ 

 


 

๐Ÿงธ ๊ตฌ์กฐํŒŒ์•… ๋ ˆ์ธ ๊ผฌ์šฐ- ๐Ÿงธ

 • scope

ใ„ด class :  SingletonTest, SingletonWithPrototypeTest1, PrototypeTest

 • web

ใ„ด class :  LogDemoController, LogDemoService

• common

ใ„ด class :  MyLogger

 


 

Q. ๋นˆ ์Šค์ฝ”ํ”„๋ž€?

A. ๋นˆ์ด ์‚ฌ์šฉ๋˜์–ด์ง€๋Š” ๋ฒ”์œ„์ด๋‹ค.

 

 

โœ”๏ธ  SingletonTest

 

โ–บ  ๊ฒฐ๊ณผ

 

์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์šด ๊ฒƒ์€ ์‹ฑ๊ธ€ํ†ค ๋นˆ ์Šค์ฝ”ํ”„๋‹ค. ์‹ฑ๊ธ€ํ†ค ๋นˆ ์Šค์ฝ”ํ”„๋Š” ์‹ฑ๊ธ€ํ†ค ๋นˆ์ด ์‹œ์ž‘๋˜์–ด ๋๋‚ ๋•Œ๊นŒ์ง€ ์œ ์ง€๋œ๋‹ค. ๋˜ ๊ฐ™์€ ์š”์ฒญ์— ๋Œ€ํ•ด ๊ฐ™์€ ์ธ์Šคํ„ด์Šค ๋นˆ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

 

 


 

 

 

โœ”๏ธ  PrototypeTest

 

@Scope("prototype")
static class PrototypeBean {

}

 

@Scope("prototype")

: ๋นˆ์˜ ์Šค์ฝ”ํ”„๋ฅผ ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. 

 

 

โ–บ  ๊ฒฐ๊ณผ

ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์š”์ฒญ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‹ฑ๊ธ€ํ†ค ๋นˆ ์Šค์ฝ”ํ”„๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์œ ์ง€๋˜์ง€๋งŒ ํ”„๋กœํ† ํƒ€์ž…์€ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…๊ณผ ์ดˆ๊ธฐํ™”(Prototype.init 2๋ฒˆ ์š”์ฒญ๋œ ๊ฒƒ์„ ๋ณด๋ฉด)๊นŒ์ง€๋งŒ ๊ด€์—ฌํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— Predestroy ๋ฉ”์„œ๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. (ํ˜„์žฌ ์‹คํ–‰ ๊ฒฐ๊ณผ์—์„œ Prototype.destroy๊ฐ€ ์žˆ์ง€๋งŒ, ๋”ฐ๋กœ ์ฝ”๋“œ์— prototypeBean1.destroy();๋ฅผ ์ ์–ด์„œ ๊ทธ๋Ÿฐ๊ฒƒ์ด๋‹ค.)

 

 

 

 


 

 

โœ”๏ธ  SingletonWithPrototypeTest1

 

โ–บ  ํ”„๋กœํ† ํƒ€์ž…๊ณผ ์‹ฑ๊ธ€ํ†ค์„ ํ•จ๊ป˜ ์“ธ ๊ฒฝ์šฐ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ 

    @Scope("singleton")
    static class ClientBean {

	private final PrototypeBean prototypeBean;
        
        @Autowired
        public ClientBean(PrototypeBean prototypeBean) {
        	this.prototypeBean = prototypeBean;
        }

        public int logic() {
            prototypeBean.addCount();
            int count = prototypeBean.getCount();
            return count;
        }
    }

 

์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์‹ฑ๊ธ€ํ†ค ํด๋ž˜์Šค์ธ ClientBean ๋‚ด๋ถ€์— ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์ด ์„ ์–ธ๋˜์—ˆ๋‹ค. ๋ชจ๋‘๊ฐ€ ์•Œ๋‹ค์‹œํ”ผ ์‹ฑ๊ธ€ํ†ค ๋นˆ์€ ์ƒ์„ฑ์‹œ์  ๋”ฑ ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ์ค‘๋ณตํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.(์ด๋ฏธ ํ• ๋‹น๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ™์€ ๋นˆ์„ ์‚ฌ์šฉํ•  ๋•Œ ๋งˆ๋‹ค ๊ฐ™์€ ์ธ์Šคํ„ด์Šค ํ• ๋‹น) ๊ทธ๋Ÿฌ๋‹ˆ ๊ทธ ๋‚ด๋ถ€์— ํ•จ๊ป˜ํ•˜๋Š” ํ”„๋กœํ† ํƒ€์ž…๋„ ๋ณธ๋ž˜์˜ ํŠน์„ฑ์ด ์•„๋‹Œ ์‹ฑ๊ธ€ํ†ค ๋นˆ์„ ๋”ฐ๋ผ๊ฐ€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

Q. ๋งŒ์•ฝ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณ„์† ํ˜ธ์ถœ๋˜๋Š” ํ”„๋กœํ† ํƒ€์ž…์€ ๋”์ด์ƒ ํ”„๋กœํ† ํƒ€์ž…์ด ์•„๋‹ˆ๊ณ  ์‹ฑ๊ธ€ํ†ค์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‚˜?

A. ๋งž๋‹ค. ๊ทธ ํŠน์„ฑ์ด ์‹ฑ๊ธ€ํ†ค์ด ๋˜์—ˆ์œผ๋‹ˆ, ๋”์ด์ƒ ํ”„๋กœํ† ํƒ€์ž…์ด ์•„๋‹ˆ๋‹ค.

 

 

 

 

โ–บ  ์ฃผ์ž… ์‹œ์  ์ฐจ์ด

static class ClientBean {
 
         @Autowired
         private ApplicationContext ac;
         
         public int logic() {
             PrototypeBean prototypeBean = ac.getBean(PrototypeBean.class);
             prototypeBean.addCount();
             int count = prototypeBean.getCount();
             return count;
	}
}

 

์ „ ์ฝ”๋“œ๋Š” ์‹ฑ๊ธ€ํ†ค ๋นˆ ๋‚ด๋ถ€์— ํ”„๋กœํ† ํƒ€์ž…์ด ์„ ์–ธ๋˜์—ˆ๋‹ค. ํ˜„์žฌ ์ฝ”๋“œ๋Š” ApplicationContext๋ฅผ ์ด์šฉํ•ด ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. logic ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ac(ApplicationContext)๋ฅผ ํ†ตํ•ด ํ”„๋กœํ† ํƒ€์ž… ๋นˆ์„ ๋งค๋ฒˆ ์š”์ฒญํ•˜๊ฒŒ ๋œ๋‹ค. 

 

 

 

 

 

โ–บ  ObjectFactory

static class ClientBean {

	@Autowired
	private ObjectProvider<PrototypeBean> prototypeBeanProvider;
 
	public int logic() {
		PrototypeBean prototypeBean = prototypeBeanProvider.getObject();
		prototypeBean.addCount();
		int count = prototypeBean.getCount();
		return count;
	}
}

 

ObjectProbvider

: ์ง€์ •ํ•œ ๋นˆ์„ ์ฐพ์•„ ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋Œ€์‹  ์ฐพ์•„์ฃผ๋Š” DL ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. / ์Šคํ”„๋ง์— ์˜์กด์ ์ด๋‹ค.

 

 

 

- ObjectProvider ์ถœ์ฒ˜

 

ObjectProvider (Spring Framework 6.1.13 API)

Return a sequential Stream over all matching object instances, pre-ordered according to the factory's common order comparator. In a standard Spring application context, this will be ordered according to Ordered conventions, and in case of annotation-based

docs.spring.io

 

- ObjectFactory ์ถœ์ฒ˜

 

ObjectFactory (Spring Framework 6.1.13 API)

Defines a factory which can return an Object instance (possibly shared or independent) when invoked. This interface is typically used to encapsulate a generic factory which returns a new instance (prototype) of some target object on each invocation. This i

docs.spring.io

 

 

 

 

โ–บ  JSR-330 Provider

static class ClientBean {

    @Autowired
    private Provider<PrototypeBean> prototypeBeanProvider;

    public int logic() {
        PrototypeBean prototypeBean = prototypeBeanProvider.get();
        prototypeBean.addCount();
        int count = prototypeBean.getCount();
        return count;
    }
}

 

Provider< >

:  < > ์•ˆ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์ง€์—ฐ, ์˜ต์…˜, ์˜์กด์ ์ธ ๊ฒฝ์šฐ์ธ ์ˆœํ™˜์ฐธ์กฐ๋ฌธ์ œ์— ์“ด๋‹ค.

 

 

- Provider ์ถœ์ฒ˜

 

Provider (Jakarta EE 8 Specification APIs)

T get() Provides a fully-constructed and injected instance of T.

jakarta.ee

 

 

 

 


 

 

 

 

โœ”๏ธ  MyLogger

๊ฐ€์งœ ํ”„๋ก์‹œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋กœ๊ทธ ์š”์ฒญ์„ ์œ„ํ•œ ํด๋ž˜์Šค

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyLogger {

 

value

: ์›น ์Šค์ฝ”ํ”„์˜ ์ข…๋ฅ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ํ˜„์žฌ๋Š” Http ์š”์ฒญ ํ•˜๋‚˜๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ  ๋‚˜๊ฐˆ๋•Œ ๊ฐ€์ง€ ์œ ์ง€๋˜๋Š” request๋กœ ๋˜์–ด์žˆ๋‹ค.

 

 

ProxyMode

: GoF ๋‹ค์ž์ธ ํŒจํ„ด์œผ๋กœ ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ด๋‚˜ ์ดˆ๊ธฐํ™”๋ฅผ ์ง€์—ฐํ•˜๊ฑฐ๋‚˜, ์ ‘๊ทผ์„ ์ œ์–ดํ•  ๋•Œ, ์„ฑ๋Šฅ ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

 

ScopedProxyMode์˜ ์ข…๋ฅ˜

- DEFAULT : ํ”„๋ก์‹ฑ ์—†์Œ
- NO : ํ”„๋ก์‹ฑ ์—†์Œ (DEFAULT์™€ ๊ฐ™์Œ)
- INTERFACE : ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์  ํ”„๋ก์‹ฑ ์ƒ์„ฑ
- TARGET_CLASS : ๋Œ€์ƒ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜์œผ๋กœ CGLIB ํ”„๋ก์‹œ ์ƒ์„ฑ
- SCOPED_PROXY INTERFACES : INTERFACE ํ˜น์€ TARGET_CLASS ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠน๋ณ„ํ•œ ๋‹จ์ถ•ํ˜• ๊ฐ’์ด๋‹ค.

 

 

๊ณ ๊ฐ์ด ์„œ๋น„์Šค๋ฅผ ์–ธ์ œ ์š”์ฒญํ• ์ง€ ๋ชจ๋ฅธ๋‹ค. ๊ฑฐ๊ธฐ๋‹ค 

 

 

 

- ProxyMode  ์ถœ์ฒ˜

 

ProxyMode in Spring Framework

The Spring Framework is a comprehensive and powerful framework for building enterprise-grade Java applications. One of its core features is…

medium.com

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

public void setRequestURL(String requestURL) {
    this.requestURL = requestURL;
}

public void log(String message) {
    System.out.println("[" + uuid + "]" + "[" + requestURL + "]" + message);
}

@PostConstruct
public void init() {
    uuid = UUID.randomUUID().toString();
    System.out.println("[" + uuid + "] request scope bean create:" + this);
}

 

setter๋ฅผ ํ†ตํ•ด requestURL์„ ๋ฐ›๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ Http ์š”์ฒญ์ด ์˜ค๋ฉด @PostConstruct๋ฅผ ํ†ตํ•ด uuid๋ฅผ random์œผ๋กœ ๋งŒ๋“ค์–ด log๋กœ ๋ณด๋‚ธ๋‹ค.

 

 

Q. requestURL์€ ๋นˆ์ด ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์„ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— setter๋กœ ๋ฐ›๋Š”๋ฐ ์™œ ์ƒ์„ฑ์‹œ์ ์„ ๋ชจ๋ฅผ๊นŒ?

A. ํ˜„์žฌ MyLogger์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, requestURL์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ๋ฐ›์•„์˜จ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ฑฐ๊ธฐ๋‹ค MyLogger์€ request ์Šค์ฝ”ํ”„๋กœ ์ •์˜๋˜์–ด์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋Š”๊ฑด Http์˜ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ๊ทธ ํ›„์— requestURL ๊ฐ’์ด ๊ฒฐ์ •๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ requestURL ๋นˆ์€ ์ƒ์„ฑ์‹œ์ ์€ ์•Œ ์ˆ˜ ์—†๋‹ค. ์ผ๋‹จ ์š”์ฒญ์ด ์žˆ์–ด์•ผํ•˜๋Š”๋ฐ ์š”์ฒญ์ด ์–ธ์ œ ์˜ฌ์ง€ ๋ชจ๋ฅด๋ฉฐ ๋‹ค๋ฅธ ํด๋ž˜์Šค(์„œ๋น„์Šค๋‚˜ ์ปจํŠธ๋กค๋Ÿฌ)์— requestURL์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

 

 


 

 

 

 

โœ”๏ธ  LogDemoController

๋กœ๊ฑฐ๊ฐ€ ์ž˜ ์ž‘๋™๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ์šฉ ์ปจํŠธ๋กค๋Ÿฌ

 

@Controller
@RequiredArgsConstructor
public class LogDemoController {

    private final LogDemoService logDemoService;
    private final MyLogger myLogger;

 

@ReqiredArgsConstructor (Lombok)

: ์ดˆ๊ธฐํ™” ๋˜์ง€์•Š์€ final ํ•„๋“œ๋‚˜, @NonNull์ด ๋ถ™์€ ํ•„๋“œ์— ๋Œ€ํ•ด ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, @Autowired๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•œ๋‹ค.

 

 

LogDemoService์™€ MyLogger ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•ด์ค€๋‹ค. private์™€ final๋กœ ์„ ์–ธํ•œ ์ด์œ ๋Š” ์ฝ๊ฑฐ๋‚˜ ์ˆ˜์ •์„ ๋ชปํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ์™ธ๋ถ€์—์„œ ์ˆ˜์ •๋  ๊ฒฝ์šฐ ์ฝ”๋“œ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

 

- @ReqiredArgsConstructor ์ถœ์ฒ˜

 

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

 

projectlombok.org

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

@RequestMapping("log-demo")
@ResponseBody
public String longDemo(HttpServletRequest request) {
    String requestURL = request.getRequestURI().toString();
    myLogger.setRequestURL(requestURL);

 

@RequestMapping( )

: ๋“ค์–ด์˜จ ์š”์ฒญ์„ ํŠน์ • ๋ฉ”์„œ๋“œ์™€ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” @RequestMapping์— ๋Œ€ํ•ด ๊ด„ํ˜ธ ์•ˆ์— ์–ด๋–ค value๊ฐ’์ด๋‚˜ method๊ฐ’์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ ‡๋‹ค๋Š”๊ฑด ์š”์ฒญ๋ฐฉ์‹์— ๊ด€๊ณ„์—†์ด url์ด ์ผ์น˜ํ•˜๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ("log-demo")๋ผ๊ณ  ์ ํ˜€์žˆ์œผ๋‹ˆ, log-demo ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜จ Http ์š”์ฒญ์„ ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋œ๋‹ค.

 

 

@ResponseBody

: Http ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜, ์‘๋‹ต ๋ฐ”๋””๋ฅผ JSON, XML, ๋ฌธ์ž์—ด ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

 

 

getRequestURL

: ์š”์ฒญ์— ์‚ฌ์šฉ๋œ URL๋กœ ๋ถ€ํ„ฐ URL ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.

 

 

toString( )

: ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

HttpServletRequest๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ Http ์š”์ฒญ์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด MyLogger์—์„œ ๊ฐ€์งœ ํ”„๋ก์‹œ๋ฅผ ๋งŒ๋“ค๋„๋ก ํ–ˆ์œผ๋‹ˆ, Http ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด logDemo์—์„œ HttpServletRequest์— ๊ฐ€์งœ ํ”„๋ก์‹œ๋ฅผ ๋„ฃ๊ณ  ์‹œ์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ ํ›„์— ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ URL ๊ฐ’์„ ๊ฐ€์ ธ์™€ ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ๋˜๋ฉด myLogger.set- ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด MyLogger์—์„œ URL ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

 

 

- @RequestMapping ์ถœ์ฒ˜

 

Spring @RequestMapping์ด๋ž€? ์ž‘์„ฑ๋ฐฉ๋ฒ•, ์˜ˆ์‹œ

1. @RequestMapping์ด๋ž€ @RequestMapping์€ ํด๋ผ์ด์–ธํŠธ์ด ์š”์ฒญ(url)์— ๋งž๋Š” ํด๋ž˜์Šค๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. ์ด ์–ด๋…ธํ…Œ์ด์…˜์€ ๊ทธ ์œ„์น˜์— ๋”ฐ๋ผ ์˜๋ฏธ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ํด๋ž˜์Šค ๋ ˆ๋ฒจ : ๊ณตํ†ต ์ฃผ์†Œ ๋ฉ”

sharonprogress.tistory.com

 

 

- @ResponseBody ์ถœ์ฒ˜

 

[Spring] @RequestBody / @ResponseBody ์–ด๋…ธํ…Œ์ด์…˜ ์ด๋ž€?

์Šคํ”„๋ง์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ @RequestBody , @ResponseBody๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ด ์–ด๋…ธํ…Œ์ด์…˜๋“ค์€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ• ๊นŒ? ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์˜ ๋น„๋™๊ธฐ ํ†ต์‹  ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ํ†ต

cheershennah.tistory.com

 

 

- getrequestURL ์ถœ์ฒ˜

 

request ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ ๊ฐ„๋‹จ์ •๋ฆฌ ๋ฐ getRequestURL()๊ณผ getRequestURI() ์ฐจ์ด

Request ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ ๊ฐ„๋‹จ์ •๋ฆฌ์ž๋ฐ” Request ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด ๋ณด์•˜๋‹ค.์ž์ฃผ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ๋งค๋ฒˆ ์ฐพ๊ฒŒ ๋˜๋Š” ํœ˜๋ฐœ์„ฑ ๊ธฐ์–ต๋ ฅ ๋•Œ๋ฌธ์— ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์•˜๋‹ค.๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ ํ•ด๋†“์œผ์‹  ๋ถ„๋“ค๋„ ๋งŽ๋‹ค๋Š” ๊ฒƒ์„

tnsgud.tistory.com

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

 

myLogger.log("controller test");
logDemoService.logic("testId");
return"OK";

 

"controller test"๋ผ๋Š” ๋ฉ”์„ธ์ง€์™€ ํ•จ๊ป˜ myLogger์— ์žˆ๋Š” log๋ฅผ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ ๋ฐ‘์— ์ค„์€ logDemoService์— logic์— "testId"๋ผ๋Š” ๋ฉ”์„ธ์ง€์™€ ํ•จ๊ป˜ logic ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. retuen "OK"๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ OK๋ผ๋Š” ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

 

 

 


 

 

 

 

โœ”๏ธ  LogDemoService

์ฃผ์–ด์ง„ id์™€ ํ•จ๊ป˜ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๋Š” ์„œ๋น„์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌ

 

private final MyLogger myLogger;

public void logic(String id) {
    myLogger.log("service id = " + id);
}

 

์•„๊นŒ LogDemoController์—  ์ฝ”๋“œ ์ชฝ์— logDemoService.logic("testId");๊ฐ€ ์žˆ์—ˆ๋‹ค. testId๋ฅผ logic ๋ฉ”์„œ๋“œ์— ๋ณด๋‚ด๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ฐ›์€ ๋ฉ”์„ธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  logic ๋ฉ”์„œ๋“œ๋Š” MyLogger์— ์žˆ๋Š” log๋ฉ”์„œ๋“œ์— id๋ฅผ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด log ๋ฉ”์„œ๋“œ์— ํ•„์š”ํ•œ uuid์™€ URL ๊ทธ๋ฆฌ๊ณ  id๊ฐ€ ๋ชจ๋‘ ์ž˜ ์ถœ๋ ฅ๋˜๊ฒŒ ๋œ๋‹ค.

 

 

 

 


 

 

 

โœ”๏ธ  ์ด์ •๋ฆฌ

 ์ด๋ฒˆ์— ๋ฐฐ์šด ๊ฒƒ์€ ํ”„๋กœํ† ํƒ€์ž… ์Šค์ฝ”ํ”„์™€ ์›น ์Šค์ฝ”ํ”„์— ๊ด€ํ•œ ๊ฒƒ์ด์—ˆ๋‹ค. ํ”„๋กœํ† ํƒ€์ž… ์Šค์ฝ”ํ”„๋Š” ์‹ฑ๊ธ€ํ†ค ์Šค์ฝ”ํ”„์™€ ๋‹ค๋ฅด๊ฒŒ ์ธ์Šคํ„ด์Šค๋ฅผ ํ•„์š”ํ•  ๋•Œ ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ๋ฐ›์•„ ์“ด๋‹ค๋Š” ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ, ์ฃผ๋กœ ์‹ฑ๊ธ€ํ†ค๊ณผ ํ•จ๊ป˜ ์“ฐ์ผ ํ”„๋กœํ† ํƒ€์ž… ์Šค์ฝ”ํ”„์—” ํฐ ๋ฌธ์ œ์ ‘์ด ์žˆ์—ˆ๋‹ค. ์‹ฑ๊ธ€ํ†ค ๋‚ด๋ถ€์— ํ”„๋กœํ† ํƒ€์ž… ์Šค์ฝ”ํ”„๋ฅผ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ์‹ฑ๊ธ€ํ†ค ์Šค์ฝ”ํ”„์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊ฐ€์ง€ ์œ ์ง€๋˜๋Š” ํŠน์„ฑ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๊ฐ€ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ทธ ๋ถ€๋ถ„์„ ์ฃผ์ž…์‹œ์ ์„ ๋‹ค๋ฅด๊ฒŒํ•˜๊ฑฐ๋‚˜, ObjectProvider๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, JSR-330 Provider๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค. (ํ•˜์ง€๋งŒ ์‚ฌ์‹ค์ƒ ์“ธ ์ผ์ด ๋งŽ์ด ์—†๋‹ค๋Š” ๊ฒƒ) 

 

 ์•„๋งˆ ์›น์Šค์ฝ”ํ”„๋Š” ์›น๊ฐœ๋ฐœ์„ ํ•˜๋ฉฐ ์‹ค๋ฌด์ ์œผ๋กœ ํ•„์š”ํ•œ ๋‚ด์šฉ์ผ ๋“ฏ ํ•˜๋‹ค. ์›น์Šค์ฝ”ํ”„๋ฅผ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋ฌธ์ œ์ ์€ request ์Šค์ฝ”ํ”„ ๋นˆ์ด ์‹ค์ œ ๊ณ ๊ฐ์˜ ์š”์ฒญ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค๋Š” ์ ์ด์—ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ฐ€์งœ ํ”„๋ก์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋‹จ ์žˆ๋Š” ์ฒ™์„ ํ•œ ๋’ค, ๋‚˜์ค‘์— ํ•„์š”ํ•œ ๋ถ€๋ถ„์—์„œ ์‹ค์ œ ์Šค์ฝ”ํ”„ ๋นˆ์ด ์žˆ๋Š” ๊ณณ์„ ํ–ฅํ•ด ์ƒ์„ฑ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. 

 

 ๋งˆ์ง€๋ง‰ ์žฅ ์ •๋ฆฌ๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค. ๋ง‰ํŒ์— ์ฝ”๋กœ๋‚˜ + ์œ„๊ฒฝ๋ จ + ์ถ”์„ ๋“ฑ ์—ฌ๋Ÿฌ ์ผ๋กœ ์ธํ•ด ์ฒœ์ฒœํžˆ ์ •๋ฆฌํ•˜๋‹ค ๊ฒฐ๊ตญ ๋‚ด์šฉ์„ ๊นŒ๋จน์–ด ๋‹ค์‹œ ๊ฐ•์˜๋ฅผ ๋“ค์–ด ๋ณต์Šตํ•˜๋ฉฐ ์ •๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. 2024๋…„ 9์›” 22์ผ ์•„์ง ์•ž์— ์ •๋ฆฌํ–ˆ๋˜ ๊ฐ•์˜๋“ค์€ ๋‹ค์‹œ ์ •๋ฆฌํ•˜์ง„ ์•Š์•˜์ง€๋งŒ, ๋น ๋ฅด๊ฒŒ ๋ณต์Šตํ•˜๋ฉฐ ์•ž์— ์ •๋ฆฌํ•ด๋‘” ๋ธ”๋กœ๊ทธ๋“ค๋„ ๋‹ค์‹œ๊ธˆ ์†์„ ๋ณผ ์˜ˆ์ •์ด๋‹ค. 

 ๋ชจ๋‘๋“ค ๊ฑด๊ฐ•ํ•˜๊ธธ ๋ฐ”๋ผ๋ฉฐ...๐ŸŽƒ 

+ Recent posts