@Configuration(proxyBeanMethods = false)
๐Ÿ™Š

@Configuration(proxyBeanMethods = false)

Created
Jul 25, 2024 02:26 AM
Last edited time
Last updated July 25, 2024
Tags
Spring
Language
Java
URL

Intro::

@Configuration(proxyBeanMethods = false) ์— ๋Œ€ํ•œ ์ •๋ฆฌ๋ณธ์ž…๋‹ˆ๋‹ค.
ย 

๊ธฐ๋ณธ์ ์ธ @Configuration ๋™์ž‘ ๋ฐฉ์‹

Spring์€ @Configuration ํด๋ž˜์Šค๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ, ํ”„๋ก์‹œ(proxy) ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ๊ฐ€๋กœ์ฑ„๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์Šคํ”„๋ง์˜ ๋นˆ ๋“ฑ๋ก ๊ณผ์ •์—์„œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } @Bean public MyRepository myRepository() { return new MyRepository(); } }
AppConfig ํด๋ž˜์Šค๋Š” ์Šคํ”„๋ง์— ์˜ํ•ด ํ”„๋ก์‹œ๋กœ ๋ž˜ํ•‘๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด myService()์™€ myRepository() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์ด ํ”„๋ก์‹œ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌ๋˜๋ฉฐ, ๋™์ผํ•œ ๋นˆ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

proxyBeanMethods ์„ค์ • ์˜ต์…˜

proxyBeanMethods ์„ค์ • ์˜ต์…˜์€ ์ด๋Ÿฌํ•œ ํ”„๋ก์‹œ ๋™์ž‘์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

proxyBeanMethods = true (๊ธฐ๋ณธ๊ฐ’)

  • ํ”„๋ก์‹œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ @Bean ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ๊ฐ€๋กœ์ฑ„๊ณ  ๋™์ผํ•œ ๋นˆ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Š” @Bean ๋ฉ”์„œ๋“œ ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

proxyBeanMethods = false

  • ํ”„๋ก์‹œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • @Bean ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ๊ฐ€๋กœ์ฑ„์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ ์ตœ์ ํ™”์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

proxyBeanMethods = false ์‚ฌ์šฉ ์˜ˆ์ œ

proxyBeanMethods = false ์„ค์ •์€ ํ”„๋ก์‹œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์ด ํ•„์š”ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, @Bean ๋ฉ”์„œ๋“œ ๊ฐ„์˜ ์˜์กด์„ฑ์ด ์—†์„ ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
@Configuration(proxyBeanMethods = false) public class AppConfig { @Bean public MyService myService() { return new MyService(); } @Bean public MyRepository myRepository() { return new MyRepository(); } }
์ด ์„ค์ •์—์„œ๋Š” myService()์™€ myRepository() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ”„๋ก์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์•ฝ๊ฐ„์˜ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ

  • proxyBeanMethods = false๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด @Bean ๋ฉ”์„œ๋“œ ๊ฐ„์˜ ํ˜ธ์ถœ์ด ํ”„๋ก์‹œ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๋นˆ ๊ฐ„์˜ ์˜์กด์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•œ @Bean ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹ค๋ฅธ @Bean ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ•ด๋‹น ๋นˆ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ, proxyBeanMethods = false ์„ค์ • ์‹œ ํ•ญ์ƒ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

  • proxyBeanMethods = true (๊ธฐ๋ณธ๊ฐ’): ์Šคํ”„๋ง์€ ํ”„๋ก์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ @Bean ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ๊ฐ€๋กœ์ฑ„๊ณ  ๋™์ผํ•œ ๋นˆ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋นˆ ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • proxyBeanMethods = false: ํ”„๋ก์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ”„๋ก์‹œ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์—ฌ ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ๋„๋ชจํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋นˆ ๊ฐ„์˜ ์˜์กด์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
proxyBeanMethods = false๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํŠน์ • ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋นˆ ๊ฐ„์˜ ์˜์กด์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.
ย 

References::

Loading Comments...