๋ฐ˜์‘ํ˜•

0.๐Ÿšถ๋“ค์–ด๊ฐ€๋ฉฐ 

Spring boot๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•Œ์•„์„œ ๋งŒ๋“ค์–ด์ง€๋Š” ์œ ์ผํ•œ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

์—ฌ๊ธฐ์— ๋ถ™์–ด์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ธ @SpringBootApplication์ด ์–ด๋–ค ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜์—ฌ ์ด์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด์•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด Spring์˜ ์—ฌ๋Ÿฌ ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ๊ทธ ๋‚ด์šฉ์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

@SpringBootApplication์ด ์–ด๋–ค ์–ด๋…ธํ…Œ์ด์…˜์ธ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด ์ •์˜๋œ ๊ณณ์„ ์ฐพ์•„๊ฐ€ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ํŒจํ‚ค์ง€์— ์กด์žฌํ•˜๋”๊ตฐ์š”!

package org.springframework.boot.autoconfigure;

@SpringBootApplication ์–ด๋…ธํ…Œ์ด์…˜์˜ ์„ ์–ธ๋ถ€๋งŒ ์‚ดํŽด๋ณด๋ฉด ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฃผ์˜ ๊นŠ๊ฒŒ ๋ด์•ผํ•  ๊ณณ์€ ์Šคํ”„๋ง ๊ณ ์œ  ์–ด๋…ธํ…Œ์ด์…˜์ด๊ณ  ์—ฌ๊ธฐ์—๋Š” ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan

์œ„ ์„ธ ๊ฐ€์ง€ ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋Šฅ์„ @SpringBootApplication ํ•˜๋‚˜๋กœ ํ™œ์„ฑํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์ฃ !

 

๊ทธ๋Ÿฌ๋ฉด ํ•˜๋‚˜ํ•˜๋‚˜ ์ข€ ๋” ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 


1. @SpringBootConfiguration

@SpringBootConfiguration

enable registration of extra beans in the context or the import of additional configuration classes. An alternative to Spring’s standard @Configuration that aids configuration detection in your integration tests.

 

์Šคํ”„๋ง์˜ @Configuration ์–ด๋…ธํ…Œ์ด์…˜๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. ๊ฑฐ์˜ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ฃ !

 

๋‘ ์–ด๋…ธํ…Œ์ด์…˜์˜ ์ฐจ์ด์ ์œผ๋กœ๋Š” @SpringBootConfiguration์€ ๊ตฌ์„ฑ์„ ์ž๋™์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

 

์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ๋Š” ์•„์ง ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ ๊ฐ™์•„ ์ถ”ํ›„ ๋” ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ๋‚ด์šฉ์„ ์กฐ๊ธˆ ๋” ๋ณด์™„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

@Configuration ์–ด๋…ธํ…Œ์ด์…˜?

 

์„ค์ • ํด๋ž˜์Šค์—์„œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด ๋นˆ์„ ๋“ฑ๋กํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

@Cofiguration ์–ด๋…ธํ…Œ์ด์…˜์„ ํด๋ž˜์Šค์— ๋ถ™์ด๋Š” ๊ฒƒ์ด๊ณ  @Bean ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์ด์ฃ .

@Configuration 
public class MyConfig {

    @Bean
    public MyBean mybean() {
        return new MyBean(); 
    } 
}

์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ๋˜๋ฉด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ @Configuration์ด ๋ถ™์–ด์žˆ๋Š” MyConfig๋ฅผ ์ž๋™์œผ๋กœ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ , ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ํŒŒ์‹ฑํ•ด์„œ @Bean์ด ๋ถ™์–ด์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์•„ ๋นˆ์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค. ์ด ๋•Œ Bean์˜ ์ด๋ฆ„์€ ๋ฉ”์„œ๋“œ๋ช…์ด ๋ฉ๋‹ˆ๋‹ค.

 

@Configuration์„ ์จ์ฃผ์–ด์•ผ @Bean์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๋นˆ์ด ์‹ฑ๊ธ€ํ†ค์„ ๋ณด์žฅ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!

 

[Spring] @Configuration ์•ˆ์— @Bean์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ , proxyBeanMethods - (2/2)

Spring์—์„œ ์ˆ˜๋™์œผ๋กœ ๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ์—๋Š” @Configuration ํด๋ž˜์Šค ์•ˆ์—์„œ @Bean์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์™œ @Configuraiton ํด๋ž˜์Šค ์•ˆ์—์„œ @Bean์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1. @Configuration.

mangkyu.tistory.com

 


2. @EnableAutoConfiguration

@EnableAutoConfiguration

enable Spring Boot’s auto-configuration mechanism

 

ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์€ ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ์ž๋™ ์„ค์ • ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ™œ์„ฑํ™”์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ด์ง€..?

 

Spring Boot์˜ ๋นˆ ๋“ฑ๋ก ๋ฐฉ์‹์€ ๋‘ ๋‹จ๊ณ„์— ๊ฑธ์ณ์„œ ์ง„ํ–‰๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  1. ComponentScan์œผ๋กœ bean์„ ๋“ฑ๋ก
  2. EnableAutoConfiguration์œผ๋กœ bean์„ ๋“ฑ๋ก

์ด ๋•Œ 2๋ฒˆ ๊ณผ์ •์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ฉด, @EnableAutoConfiguration ์–ด๋…ธํ…Œ์ด์…˜์€ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด๋†“์€ ๋นˆ ์„ค์ •๋“ค(meta ํŒŒ์ผ์— ์กด์žฌ)์„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

# Spring Boot์˜ meta ํŒŒ์ผ? -> spring-boot-autoconfigure ํ”„๋กœ์ ํŠธ์˜ META-INF ํŒจํ‚ค์ง€ ์•ˆ spring.factories์— ๋“ค์–ด์žˆ์Œ!


3. @ComponentScan

@ComponentScan

enable @Component scan on the package where the application is located

 

์ž์ฃผ ๋ณด์ด๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ๋‚˜์™”๋„ค์š”,, ์•ž์„œ ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ๋นˆ ๋“ฑ๋ก์„ ํ•œ๋‹ค๊ณ  ํ•˜์˜€๋Š”๋ฐ ์ข€ ๋” ์•Œ์•„๋ด…์‹œ๋‹ค.

 

์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ํ•„์š”์— ๋”ฐ๋ผ @Component, @Service, @Repository, @Controller, @Configuration ๋“ฑ๊ณผ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. 

 

๊ทผ๋ฐ ๊ทธ๋ƒฅ ๋‚ด๊ฐ€ ๋งŒ๋“  ํด๋ž˜์Šค์— ์œ„์™€ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์“ฐ๋ฉด ์„ค์ • ํŒŒ์ผ์— ๋“ฑ๋กํ•œ ๊ฒƒ๋„ ์•„๋‹Œ๋ฐ ์–ด๋–ป๊ฒŒ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์ค„๊นŒ?๋ผ๋Š” ์˜๋ฌธ์ด ์ƒ๊น๋‹ˆ๋‹ค. ๋ฐ”๋กœ @ComponentScan์ด ์„ค์ •๋œ ์‹œ์ž‘ ์ง€์ ๋ถ€ํ„ฐ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ ์žํ•˜๋Š” ๋†ˆ๋“ค(์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค๋“ค)์„ ๋‹ค ์ฐพ์•„์„œ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค. 

 

์œ„์—์„œ ์‚ดํŽด๋ณธ ๊ฒƒ๊ณผ ๊ฐ™์ด @Configuration์—์„œ ์ˆ˜๋™์œผ๋กœ ๋นˆ์„ ๋“ฑ๋กํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ(@Bean ์‚ฌ์šฉํ•ด์„œ) ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ์ž๋™์œผ๋กœ Component๋“ค์„ Scan ํ•˜์—ฌ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.

@Configuration 
@ComponentScan
public class MyConfig {
	// Bean๋“ค์ด ์ž๋™์œผ๋กœ ๋“ฑ๋ก
}

4.๐Ÿ“š์ •๋ฆฌ

์ •๋ฆฌํ•˜์ž๋ฉด @SpringBootApplication์„ ํ†ตํ•ด

  1. @SpringBootConfiguration 
  2. @EnableAutoConfiguration
  3. @ComponentScan 

์œ„ ์„ธ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ํ•œ๊บผ๋ฒˆ์— ์ œ๊ณตํ•ด์ค€๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•„์š”ํ•œ ๋นˆ๋“ค์„ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด ์ฃผ๋œ ๊ธฐ๋Šฅ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 


๊ด€๋ จ ๋‚ด์šฉ์„ ์ฐพ๋‹ค๋ณด๋‹ˆ ์Šคํ”„๋ง ๊ธฐ๋ณธ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ž˜ ์•Œ์ง€ ๋ชปํ•˜๋Š” ๊ตฌ๋‚˜๋ผ๊ณ  ๋Š๋ผ๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ ์Šคํ”„๋ง ๊ณต๋ถ€๋ฅผ ๋” ์ง„ํ–‰ํ•ด๋ณด๋ฉด์„œ ๊ธฐํšŒ๊ฐ€ ๋˜๋ฉด ๊ด€๋ จ ๋‚ด์šฉ์„ ์กฐ๊ธˆ ๋” ์ž์„ธํ•˜๊ฒŒ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

 

 

๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค!