Skip to content

Latest commit

ย 

History

History
93 lines (69 loc) ยท 4.21 KB

@SpringBootApplication.md

File metadata and controls

93 lines (69 loc) ยท 4.21 KB

@SpringBootApplication

SpringBoot๋Š” main ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

SpringBoot ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑ์‹œ ํ”„๋กœ์ ํŠธ ๋ช…์œผ๋กœ @SpringBootApplication ์ด ๋ถ™์€ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜๊ณ  ๊ทธ ์•ˆ์—๋Š” main ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

@SpringBootApplication
public class SpringTestApplication {

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

}

@SpringBootApplication ์–ด๋…ธํ…Œ์ด์…˜์€ ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์„ค์ •์„ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด @SpringBootApplication์ด ์–ด๋–ป๊ฒŒ ๊ธฐ๋ณธ์ ์ธ ์„ค์ •์„ ํ•˜๋Š”์ง€, ์–ด๋–ป๊ฒŒ Spring ๋‚ด๋ถ€์—์„œ ์ž‘๋™์„ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ @SpringBootApplication ์˜ ์†Œ์Šค์ฝ”๋“œ ์ค‘ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

	// ... 
}

์–ด๋…ธํ…Œ์ด์…˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„  Target๊ณผ Retention์„ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์€ ๊ธฐ๋ณธ์ž…๋‹ˆ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒƒ์„ ์‚ดํŽด๋ณผ๊นŒ์š”?

@SpringBootConfiguration

Spring Boot ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ @Configuration์„ ์ œ๊ณตํ•จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ตฌ์„ฑ์„ ์ž๋™์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก Spring์˜ ํ‘œ์ค€ @Configuration ์ฃผ์„ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@ComponentScan

์ด ์–ด๋…ธํ…Œ์ด์…˜์€ @component ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐ @Service, @Repository, @Controller ๋“ฑ์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ ์Šค์บ”ํ•˜์—ฌ Spring Bean์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.

@EnableAutoConfiguration

ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์€ ์‚ฌ์ „์— ์ •์˜ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. ๋‚ด๋ถ€์†Œ์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

	Class<?>[] exclude() default {};

	String[] excludeName() default {};

}
  • @ComponentScan ์„ ํ†ตํ•ด ๋Œ€์ƒ์ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ Bean์„ ๋“ฑ๋กํ•˜๊ณ , tomcat ๋“ฑ ์Šคํ”„๋ง์— ์ •์˜ํ•œ ์™ธ๋ถ€ ์˜์กด์„ฑ์„ ๊ฐ–๋Š” class๋“ค์„ ์Šค์บ”ํ•˜์—ฌ bean ์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ๋•Œ ์ •์˜๋œ class๋“ค์€ spring-boot-autoconfigure ์•ˆ์— ๋“ค์–ด์žˆ๋Š” META-INF ํด๋”์— spring.factories ๋ผ๋Š” ํŒŒ์ผ์— ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด ์ค‘์—์„œ org.springframework.boot.autoconfigure.EnableAutoConfiguration์„ key๋กœํ•˜๋Š” ์™ธ๋ถ€ ์˜์กด์„ฑ class๋“ค์ด ์กด์žฌํ•˜๋ฉฐ, ์—ฌ๊ธฐ์— ์ •์˜๋œ ๋ชจ๋“  class๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ class์— ๋‚ด๋ถ€์— ์ •์˜๋œ ์–ด๋…ธํ…Œ์ด์…˜์— ๋”ฐ๋ผ ๊ทธ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” class๋“ค๋งŒ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
    • @ConditionalOnProperty, @ConditionalOnClass, @ConditionalOnBean ๋“ฑ
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
...

์ •๋ฆฌ

๋”ฐ๋ผ์„œ @SpringBootApplication์ด ๋ถ™์€ ํด๋ž˜์Šค๊ฐ€ ์‹คํ–‰๋  ๋•Œ @ComponentScan์ด ํ•ด๋‹น๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ์ฝ์–ด๋“ค์—ฌ bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ , @EnableAutoConfiguration ์— ์˜ํ•ด์„œ ํ™˜๊ฒฝ์— ํ•„์š”ํ•œ ์ž๋™ ์„ค์ •์„ bean์œผ๋กœ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.