Spring ๊ณผ SpringBoot ์˜ ์ฐจ์ด

10897 ๋‹จ์–ด SpringSpringBoot๊ตฌ๋ณ„
๋ณธ ํŠœ ํ† ๋ฆฌ ์–ผ ์—์„œ ์šฐ ๋ฆฌ ๋Š” ํ‘œ์ค€ Spring ํ”„๋ ˆ์ž„ ์›Œ ํฌ ์™€ Spring Boot ๊ฐ„ ์˜ ์ฐจ ์ด ๋ฅผ ์—ฐ๊ตฌ ํ•  ๊ฒƒ ์ด๋‹ค.
MVC ์™€ Security ๊ฐ™์€ Spring ๋ชจ๋“ˆ ์€ ํ•ต์‹ฌ Spring ์—์„œ ์‚ฌ์šฉ ํ•  ๋•Œ ์™€ Boot ์—์„œ ์‚ฌ์šฉ ํ•  ๋•Œ์˜ ์ฐจ์ด ์  ์— ์ค‘์  ์„ ๋‘ ๊ณ  ํ† ๋ก  ํ•  ๊ฒƒ ์ž… ๋‹ˆ ๋‹ค.
Spring ์ด ๋ญ ์˜ˆ์š”?
์‰ฝ๊ฒŒ ๋ง ํ•˜๋ฉด Spring ํ”„๋ ˆ์ž„ ์›Œ ํฌ ๋Š” ์ž๋ฐ” ์• ํ”Œ ๋ฆฌ ์ผ€ ์ด ์…˜ ๊ฐœ๋ฐœ ์— ์ „๋ฉด์  ์ธ ์ธ ์ธ ํ”„ ๋ผ ์ง€์› ์„ ์ œ๊ณต ํ–ˆ๋‹ค.
๊ทธ๊ฒƒ ์€ ์ฃผ์ž… ์— ์˜์กด ํ•˜๊ณ  ๊ธฐ ์กด์˜ ๋ชจ๋“ˆ,์˜ˆ ๋ฅผ ๋“ค ์–ด:
  • Spring JDBC
  • Spring MVC
  • Spring Security
  • Spring AOP
  • Spring ORM
  • Spring Test
  • ์ด ๋ชจ๋“ˆ ๋“ค ์€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐœ๋ฐœ ์‹œ๊ฐ„ ์„ ํฌ๊ฒŒ ๋‹จ์ถ• ์‹œ ํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
    ์˜ˆ ๋ฅผ ๋“ค ์–ด ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์ดˆ๊ธฐ ์— ์šฐ ๋ฆฌ ๋Š” ๋Œ€๋Ÿ‰์˜ ๋ชจ๋ธ ์ฝ”๋“œ ๋ฅผ ์ž‘์„ฑ ํ•˜์—ฌ ๊ธฐ๋ก ์„ ๋ฐ์ดํ„ฐ ์›๋ณธ ์— ์‚ฝ์ž… ํ•ด ์•ผ ํ–ˆ๋‹ค.springjdbc ๋ชจ๋“ˆ ์˜ JDBCtemplate ๋ฅผ ์‚ฌ์šฉ ํ•˜๋ฉด ๋ช‡ ์ค„ ์ฝ”๋“œ ๋กœ ๊ฐ„๋žตํ™” ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    Spring Boot ๊ฐ€ ๋ญ ์˜ˆ์š”?
    Spring Boot ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Spring ํ”„๋ ˆ์ž„ ์›Œ ํฌ ์˜ ํ™•์žฅ ์œผ๋กœ Spring ํ”„๋กœ๊ทธ๋žจ ์„ค์ • ์— ํ•„์š” ํ•œ ๋ชจ๋ธ ์„ค์ • ์„ ์ œ๊ฑฐ ํ•ฉ ๋‹ˆ ๋‹ค.
    ์ด ๋Š” Spring ํ”Œ๋žซํผ ์— ๋Œ€ํ•ด ์ž์‹ ์˜ ์˜๊ฒฌ ์„ ๊ณ ์ง‘ ํ•˜ ๋ฉฐ ๋”์šฑ ๋น  ๋ฅด ๊ณ  ํšจ์œจ ์  ์ธ ์ƒํƒœ ์‹œ์Šคํ…œ ๊ฐœ๋ฐœ ์„ ์œ„ํ•ด ๊ธธ ์„ ๋‹ฆ ์•˜ ๋‹ค.
    ๋‹ค์Œ์€ Spring Boot ์˜ ์ผ๋ถ€ ๊ธฐ๋Šฅ ์ž… ๋‹ˆ ๋‹ค.
  • ์„ค์ • ๋ณด๋‹ค ์ข‹ ์€'starter'์˜์กด ๊ด€ ๊ณ„ ๋ฅผ ๊ฐ€์ง€ ๊ณ  ๊ตฌ์ถ• ๊ณผ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์„ค์ • ์„ ๊ฐ„์†Œํ™” ํ•ฉ ๋‹ˆ ๋‹ค
  • ๋‚ด์žฅ ํ˜• ์„œ๋ฒ„ ๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๋ฐฐ์น˜ ์˜ ๋ณต์žก์„ฑ ์„ ํ”ผ ํ–ˆ๋‹ค
  • ๋„๋Ÿ‰,์šดํ–‰ ์ƒํ™ฉ ๊ฒ€์‚ฌ ์™€ ์™ธ๋ถ€ ํ™” ์„ค์ •
  • ์ž๋™ ์„ค์ •-๊ฐ€๋Šฅํ•˜๋ฉด
  • ์šฐ๋ฆฌ ๋กœ ํ•˜์—ฌ ๊ธˆ ์ด ๋‘ ํ‹€ ์„ ์ ์ฐจ ์ตํžˆ ๊ฒŒ ํ•˜ ๋‹ค.
    Maven ์˜์กด ํ•ญ
    ์šฐ์„ ,Spring ์„ ์‚ฌ์šฉ ํ•˜์—ฌ ์›น ํ”„๋กœ๊ทธ๋žจ ์„ ๋งŒ ๋“œ ๋Š” ๋ฐ ํ•„์š” ํ•œ ์ตœ์†Œ ์˜์กด์„ฑ ์„ ์‚ด ํŽด ๋ณด ์ž.
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.5</version>
    </dependency>
    Spring ๊ณผ ๋‹ฌ๋ฆฌ Spring Boot ๋Š” ์˜์กด ํ•ญ๋ชฉ ํ•˜๋‚˜๋งŒ ์žˆ ์œผ ๋ฉด ์›น ํ”„๋กœ๊ทธ๋žจ ์„ ์‹œ์ž‘ ํ•˜๊ณ  ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.4.4</version>
    </dependency>
    ๊ตฌ์ถ• ๊ธฐ๊ฐ„ ์— ๋ชจ๋“  ์˜์กด ํ•ญ๋ชฉ ์ด ์ตœ์ข… ์••์ถ• ํŒŒ์ผ ์— ์ž๋™ ์œผ๋กœ ์ถ” ๊ฐ€ ๋ฉ ๋‹ˆ ๋‹ค.
    ๋˜ ํ•˜๋‚˜์˜ ์ข‹ ์€ ์˜ˆ ๋Š” ํ…Œ์ŠคํŠธ ๋ผ ์ด๋ธŒ ๋Ÿฌ ๋ฆฌ ๋‹ค.์šฐ ๋ฆฌ ๋Š” ๋ณดํ†ต Spring-Test,JUnit,Hamcrest,Mockito ๋ผ ์ด๋ธŒ ๋Ÿฌ ๋ฆฌ ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.Spring ํ”„๋กœ์ ํŠธ ์— ์„œ ๋Š” ์˜์กด ํ•ญ๋ชฉ ์œผ๋กœ ๋ชจ๋“  ๋ผ ์ด๋ธŒ ๋Ÿฌ ๋ฆฌ ๋ฅผ ์ถ”๊ฐ€ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.
    ๋˜๋Š” springboot ์—์„œ starter ์˜์กด ํ•ญ๋ชฉ ๋งŒ ์žˆ ์œผ ๋ฉด ์ด ๋ผ ์ด๋ธŒ ๋Ÿฌ ๋ฆฌ ๋ฅผ ์ž๋™ ์œผ๋กœ ํฌํ•จ ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    springboot ์€ ์„œ๋กœ ๋‹ค๋ฅธ Spring ๋ชจ๋“ˆ ์— ๋งŽ์€ ์‹œ์ž‘ ํ”„๋กœ๊ทธ๋žจ ์˜์กด ํ•ญ๋ชฉ ์„ ์ œ๊ณต ํ•ฉ ๋‹ˆ ๋‹ค.๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ ํ•˜ ๋Š” ๋ฐฉ๋ฒ• ์€:
  • spring-boot-starter-data-jpa
  • spring-boot-starter-security
  • spring-boot-starter-test
  • spring-boot-starter-web
  • spring-boot-starter-thymeleaf
  • starters ์˜ ์ „์ฒด ๋ชฉ๋ก ์„ ์–ป ์œผ ๋ ค ๋ฉด Spring ๋ฌธ ์„œ ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter
    MVC ์„ค์ •
    Spring ๊ณผ SpringBoot ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ jsp ์›น ํ”„๋กœ๊ทธ๋žจ ์„ ๋งŒ ๋“œ ๋Š” ๋ฐ ํ•„์š” ํ•œ ์„ค์ • ์„ ์—ฐ๊ตฌ ํ•ฉ ์‹œ๋‹ค.
    Spring ์€ dispatcherservlet,๋งต ๋ฐ ๊ธฐํƒ€ ์ง€์› ์„ค์ • ์„ ์ •์˜ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.์›น.xml ํŒŒ์ผ ์ด๋‚˜ ์ดˆ๊ธฐ ๊ฐ’ ์œผ๋กœ ํ•ญ๋ชฉ ํด๋ž˜์Šค ๋ฅผ ์„ค์ • ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    
    public class MyWebAppInitializer implements WebApplicationInitializer {
     
      @Override
      public void onStartup(ServletContext container) {
        AnnotationConfigWebApplicationContext context
         = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.baeldung");
     
        container.addListener(new ContextLoaderListener(context));
     
        ServletRegistration.Dynamic dispatcher = container
         .addServlet("dispatcher", new DispatcherServlet(context));
         
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
      }
    }
    @EnableWebMvc ์ฃผ์„ ์„@Configuration ํด๋ž˜์Šค ์— ์ถ”๊ฐ€ ํ•˜๊ณ  ์ปจํŠธ๋กค ๋Ÿฌ ์—์„œ ๋Œ์•„ ์˜ค ๋Š” ๋ณด ๊ธฐ ๋ฅผ ํ•ด์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋ณด๊ธฐ ํ•ด์„ ๊ธฐ ๋ฅผ ์ •์˜ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.
    
    @EnableWebMvc
    @Configuration
    public class ClientWebConfig implements WebMvcConfigurer { 
      @Bean
      public ViewResolver viewResolver() {
       InternalResourceViewResolver bean
        = new InternalResourceViewResolver();
       bean.setViewClass(JstlView.class);
       bean.setPrefix("/WEB-INF/view/");
       bean.setSuffix(".jsp");
       return bean;
      }
    }
    ์ด์— ๋น„ํ•ด ์›น starter ๋ฅผ ์ถ”๊ฐ€ ํ•œ ํ›„ Spring Boot ๋Š” ๋ช‡ ๊ฐ€์ง€ ์†์„ฑ ๋งŒ ์žˆ ์œผ ๋ฉด ์ž‘์—… ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    auto-configuration ์ด๋ผ๋Š” process ๋ฅผ ํ†ต ํ•ด bootwebstarter ๋ฅผ ์ถ”๊ฐ€ ํ•˜๋ฉด ์œ„์˜ ๋ชจ๋“  Spring ์„ค์ • ์ด ์ž๋™ ์œผ๋กœ ํฌ ํ•จ ๋ฉ ๋‹ˆ ๋‹ค.
    ์ด๊ฒƒ ์€ springboot ๊ฐ€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์— ์กด์žฌ ํ•˜ ๋Š” ์˜์กด ํ•ญ๋ชฉ,์†์„ฑ,bean ์„ ๋ณด๊ณ  ์ด ๋ฅผ ๋ฐ”ํƒ• ์œผ๋กœ ์„ค์ • ์„ ์‚ฌ์šฉ ํ•œ ๋‹ค ๋Š” ๊ฒƒ ์„ ์˜๋ฏธ ํ•ฉ ๋‹ˆ ๋‹ค.
    ๋ฌผ๋ก  ์‚ฌ์šฉ์ž ์ •์˜ ์„ค์ • ์„ ์ถ”๊ฐ€ ํ•˜๋ ค ๋ฉด Spring-boot ์ž๋™ ์„ค์ • ์ด ์ข… ๋ฃŒ ๋ฉ ๋‹ˆ ๋‹ค.
    ํ…œ ํ”Œ ๋ฆฟ ์—”์ง„ ์„ค์ •
    ์ด์ œ ์Šคํ”„๋ง ๊ณผ springboot ์— Thymeleaf ํ…œ ํ”Œ ๋ฆฟ ์—”์ง„ ์„ ์„ค์ • ํ•˜ ๋Š” ๋ฐฉ๋ฒ• ์„ ๋ฐฐ ์›Œ ๋ด… ์‹œ๋‹ค.
    Spring ์— ์„œ ๋Š” ๋ณด๊ธฐ ํ•ด์ƒ๋„ ๊ธฐ ์— thymeleaf-spring 5 ์˜์กด ํ•ญ๋ชฉ ๊ณผ ์„ค์ • ์„ ์ถ”๊ฐ€ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.
    
    @Configuration
    @EnableWebMvc
    public class MvcWebConfig implements WebMvcConfigurer {
    
      @Autowired
      private ApplicationContext applicationContext;
    
      @Bean
      public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = 
         new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        return templateResolver;
      }
    
      @Bean
      public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
      }
    
      @Override
      public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
      }
    }
    SpringBoot ๋Š” springbootstarter thymeleaf ์˜ ์˜์กด์„ฑ ๋งŒ ์žˆ ์œผ ๋ฉด ์›น ํ”„๋กœ๊ทธ๋žจ ์—์„œ thymeleaf ์ง€์› ์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.Thymeleaf 3.0 ์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ ๋•Œ๋ฌธ์—,์šฐ ๋ฆฌ ๋Š” springboot2web ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์— thymeleaf-layout-dialect ๋ฅผ ์˜์กด ํ•ญ๋ชฉ ์œผ๋กœ ์ถ”๊ฐ€ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.๋˜๋Š”,์šฐ ๋ฆฌ ๋Š” springbootstarter ์™€ eleaf ์˜์กด ๋„ ๋ฅผ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.์ด๊ฒƒ ์€ ์šฐ๋ฆฌ ์— ๊ฒŒ ๋ชจ๋“  ๊ฒƒ ์„ ์ฒ˜๋ฆฌ ํ•ด ์ค„ ๊ฒƒ ์ž… ๋‹ˆ ๋‹ค.
    ์˜์กด ํ•ญ๋ชฉ ์ด ์ž๋ฆฌ ์— ์žˆ ์œผ ๋ฉด src/main/resources/templates ํด ๋” ์— ํ…œ ํ”Œ ๋ฆฟ ์„ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.Spring ๊ฐ€ ์ด ๋“œ ๋Š” ์ž๋™ ์œผ๋กœ ํ‘œ ์‹œ ๋ฉ ๋‹ˆ ๋‹ค.
    ๋ด„ ๋ณด์•ˆ ์„ค์ •
    ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด ์„œ ์ด ํ”„๋ ˆ์ž„ ์›Œ ํฌ ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ ๊ธฐ๋ณธ HTTP ๊ธฐ๋ณธ ์ธ์ฆ ์„ ์‚ฌ์šฉ ํ•˜ ๋Š” ๋ฐฉ๋ฒ• ์„ ๋ณผ ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    Spring ์„ ์‚ฌ์šฉ ํ•˜์—ฌ ๋ณด์•ˆ ์„ ์‚ฌ์šฉ ํ•˜ ๋Š” ๋ฐ ํ•„์š” ํ•œ ์˜์กด ํ•ญ๋ชฉ ๊ณผ ์„ค์ • ์„ ์‚ด ํŽด ๋ณด ๊ฒ  ์Šต ๋‹ˆ ๋‹ค.
    Spring ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์•ˆ์ „์„ฑ ์„ ์„ค์ • ํ•˜๊ธฐ ์œ„ํ•ด ํ‘œ์ค€ springsecurity web ๊ณผ springsecurity config ์˜์กด ํ•ญ๋ชฉ ์ด ํ•„์š” ํ•ฉ ๋‹ˆ ๋‹ค.
    ๋‹ค์Œ์€ ์›น ๋ณด์•ˆ ConfigureAdapter ๋ฅผ ํ™•์žฅ ํ•˜๊ณ @EnableWebSecurity ์ฃผ์„ ์„ ์‚ฌ์šฉ ํ•  ํด๋ž˜์Šค ๋ฅผ ์ถ”๊ฐ€ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.
    
    @Configuration
    @EnableWebSecurity
    public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
     
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
         .withUser("user1")
          .password(passwordEncoder()
          .encode("user1Pass"))
         .authorities("ROLE_USER");
      }
     
      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
         .anyRequest().authenticated()
         .and()
         .httpBasic();
      }
      
      @Bean
      public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
      }
    }
    ์ธ์ฆ ์„ ์„ค์ • ํ•˜๊ธฐ ์œ„ํ•ด inMemory Authentication ์„ ์‚ฌ์šฉ ํ•ฉ ๋‹ˆ ๋‹ค.
    springboot ์€ ์ด๋Ÿฌํ•œ ์˜์กด ํ•ญ๋ชฉ ์ด ์žˆ์–ด ์•ผ ์ž‘์—… ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.๊ทธ๋Ÿฌ๋‚˜ spring-boot-starter-security ์˜ ์˜์กด ํ•ญ๋ชฉ ๋งŒ ์ •์˜ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.์ด ๋Š” ๋ชจ๋“  ์˜์กด ํ•ญ๋ชฉ ์„ classpath ๋ฅ˜ ๊ฒฝ๋กœ ์— ์ž๋™ ์œผ๋กœ ์ถ”๊ฐ€ ํ•˜๊ธฐ ๋•Œ ๋ฌธ ์ž… ๋‹ˆ ๋‹ค.
    springboot ์˜ security ๋ณด์•ˆ ์„ค์ • ์€ ์œ„ ์™€ ๊ฐ™ ์Šต ๋‹ˆ ๋‹ค.
    Spring ๊ณผ Spring ์•ˆ๋‚ด ์—์„œ JPA ์„ค์ • ์„ ์–ด๋–ป๊ฒŒ ์‹คํ˜„ ํ•˜ ๋Š” ์ง€ ์•Œ ๊ณ  ์‹ถ ์œผ ๋ฉด,์šฐ๋ฆฌ ์˜ ๊ธ€ A Guide To JPA with Spring:https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa์„ ๋ณผ ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    Application Bootstrap
    Spring ๊ณผ Spring ๊ฐ€์ด๋“œ ์—์„œ ํ”„๋กœ๊ทธ๋žจ ์„ ์œ ๋„ ํ•˜ ๋Š” ๊ธฐ๋ณธ ์  ์ธ ์ฐจ ์ด ๋Š” servlet ์— ์žˆ๋‹ค.Spring ์€ ์›น.xml ๋˜๋Š” SpringServletContainer Initializer ๋ฅผ ์•ˆ๋‚ด ์ž…๊ตฌ ๋กœ ์‚ฌ์šฉ ํ•ฉ ๋‹ˆ ๋‹ค.
    ํ•œํŽธ,SpringBoot ๋Š” Servlet 3 ๊ธฐ๋Šฅ ๋งŒ ์‚ฌ์šฉ ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์„ ์œ ๋„ ํ•ฉ ๋‹ˆ ๋‹ค.์šฐ๋ฆฌ ์ž์„ธํžˆ ์ด์•ผ๊ธฐ ํ•ฉ ์‹œ๋‹ค.
    Spring ์€ ์–ด๋–ป๊ฒŒ ์ธ๋„ ํ•ฉ ๋‹ˆ๊นŒ?
    Spring ์€ ์ „ํ†ต ์  ์ธ ์›น.xml ์•ˆ๋‚ด ๋ฐฉ์‹ ๊ณผ ์ตœ์‹  Servlet 3+๋ฐฉ๋ฒ• ์„ ์ง€์› ํ•ฉ ๋‹ˆ ๋‹ค.
    ์›น.xml ๋‹จ๊ณ„๋ณ„ ์ ‘๊ทผ ์„ ๋ณด ์—ฌ ์ค ๋‹ˆ ๋‹ค.
    1.Servlet ์šฉ๊ธฐ(์„œ๋ฒ„)์›น.xml ์ฝ ๊ธฐ.
    2.Dispatcher Servlet ์ • ์˜ ๋Š” ์›น.xml ์—์„œ ์šฉ๊ธฐ ๋กœ ์˜ˆํ™” ํ•ฉ ๋‹ˆ ๋‹ค.
    3.DispatcherServlet ๋Š” WEB-INF/{servletName}์„ ์ฝ ์–ด ์›น ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ Context-servlet.xml ์„ ๋งŒ ๋“ญ ๋‹ˆ ๋‹ค.
    4.๋งˆ์ง€๋ง‰ ์œผ๋กœ Dispatcher Servlet ๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์ปจ ํ…์ŠคํŠธ ์— ์ • ์˜ ๋œ bean ์— ๋“ฑ๋ก ํ•ฉ ๋‹ˆ ๋‹ค.
    ๋‹ค์Œ์€ ์–ด๋–ป๊ฒŒ Servlet 3+๋ฐฉ๋ฒ• ์œผ๋กœ Spring ์•ˆ ๋‚ด ๋ฅผ ์ง„ํ–‰ ํ•ฉ ๋‹ˆ๊นŒ?
    1.์šฉ๊ธฐ ๊ฒ€์ƒ‰ ์€ ServletContainer Initializer ์˜ ํด๋ž˜์Šค ๋ฅผ ์‹คํ–‰ ํ•ฉ ๋‹ˆ ๋‹ค.
    2.SpringServletContainer Initializer ๋Š” ์›น ์• ํ”Œ ๋ฆฌ ์ผ€ ์ด ์…˜ Initializer ๋ฅผ ์‹คํ˜„ ํ•˜ ๋Š” ๋ชจ๋“  ์ข… ๋ฅ˜ ๋ฅผ ์ฐพ ์Šต ๋‹ˆ ๋‹ค.
    3.WebapplicationInitializer ๋Š” XML ๋˜๋Š”@Configuration ๋ฅ˜ ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ ์ปจ ํ…์ŠคํŠธ ๋ฅผ ๋งŒ ๋“ญ ๋‹ˆ ๋‹ค.
    4.WebapplicationInitializer ๋Š” ์ด์ „์— ๋งŒ ๋“  ์ปจ ํ…์ŠคํŠธ ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ Dispatcher Servlet ์„ ๋งŒ ๋“ญ ๋‹ˆ ๋‹ค.
    ์–ด๋–ป๊ฒŒ ์Šคํ”„๋ง ๋ถ€ ํŠธ ๋ฅผ ์‹œ์ž‘ ํ•ฉ ๋‹ˆ๊นŒ?
    Spring Boot ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ์ž…๊ตฌ ์  ์€@SpringBootApplication ์ฃผ์„ ์„ ์‚ฌ์šฉ ํ•˜ ๋Š” ํด๋ž˜์Šค ์ž… ๋‹ˆ ๋‹ค.
    
    @SpringBootApplication
    public class Application {
      public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
      }
    }
    ๊ธฐ๋ณธ ์  ์ธ ์ƒํ™ฉ ์—์„œ springboot ์€ ๋‚ด์žฅ ํ˜• ์šฉ ๊ธฐ ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์„ ์‹คํ–‰ ํ•ฉ ๋‹ˆ ๋‹ค.์ด ์˜ˆ ์—์„œ springboot ๋Š” Public static void ๋ฉ” ์ธ ์ž…๊ตฌ ์  ์„ ์‚ฌ์šฉ ํ•˜์—ฌ ๋‚ด์žฅ ์›น ์„œ๋ฒ„ ๋ฅผ ์‹œ์ž‘ ํ•ฉ ๋‹ˆ ๋‹ค.
    ๋˜ํ•œ Servlet,Filter,servlet ContextInitializerbean ์„ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ์ƒํ•˜ ๋ฌธ ์—์„œ ์‚ฝ์ž… ์‹ Servlet ์šฉ๊ธฐ ๋กœ ์—ฐ๊ฒฐ ํ•˜ ๋Š” ์ผ ๋„ ๋งก ๋Š” ๋‹ค.
    springboot ์˜ ๋˜ ๋‹ค๋ฅธ ํŠน์„ฑ ์€ ์ฃผ ํด๋ž˜์Šค ์˜ ๊ฐ™์€ ๊ฐ€๋ฐฉ ์ด๋‚˜ ํ•˜์œ„ ํŒจํ‚ค์ง€ ์˜ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๊ตฌ์„ฑ ์š” ์†Œ ๋ฅผ ์ž๋™ ์œผ๋กœ ๊ฒ€์ƒ‰ ํ•˜ ๋Š” ๊ฒƒ ์ž… ๋‹ˆ ๋‹ค.
    ๋˜ํ•œ,springboot ๋Š” ์™ธ๋ถ€ ์šฉ๊ธฐ ์— ์„ค ์น˜ ๋œ ์›น ์••์ถ• ํŒŒ์ผ ์— ๋ฐฐ์น˜ ํ•˜ ๋Š” ์˜ต์…˜ ๋„ ์ œ๊ณต ํ•ฉ ๋‹ˆ ๋‹ค.์ด๋Ÿฌํ•œ ์ƒํ™ฉ ์—์„œ ์šฐ ๋ฆฌ ๋Š” SpringBoot ServletInitializer ๋ฅผ ํ™•์žฅ ํ•ด ์•ผ ํ•ฉ ๋‹ˆ ๋‹ค.
    
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
      // ...
    }
    ์—ฌ๊ธฐ ์„œ ์™ธ๋ถ€ servlet ์šฉ ๊ธฐ ๋Š” ์›น ์— ์ € ์žฅ ๋œ META-INF ํŒŒ์ผ ์—์„œ ์ • ์˜ ๋œ ์ฃผ ์ข… ๋ฅ˜ ๋ฅผ ์ฐพ ์Šต ๋‹ˆ ๋‹ค.SpringBootServletInitializer ๋Š” servlet,ํ•„ํ„ฐ,ServletContextInitializer ๋ฅผ ์—ฐ๊ฒฐ ํ•ฉ ๋‹ˆ ๋‹ค.
    ํฌ์žฅ ๋ฐ ๋ฐฐ์น˜
    ๋งˆ์ง€๋ง‰ ์œผ๋กœ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์„ ์–ด๋–ป๊ฒŒ ํฌ์žฅ ํ•˜๊ณ  ๋ฐฐ์น˜ ํ•˜ ๋Š” ์ง€ ๋ณด ์—ฌ ์ค ๋‹ˆ ๋‹ค.์ด ๋‘ ํ”„๋ ˆ์ž„ ์›Œ ํฌ ๋Š” ๋ชจ๋‘ Maven ๊ณผ Gradle ๋“ฑ ํ”ํžˆ ๋ณผ ์ˆ˜ ์žˆ ๋Š” ๊ฐ€๋ฐฉ ๊ด€๋ฆฌ ๊ธฐ์ˆ  ์„ ์ง€์› ํ•œ๋‹ค.๊ทธ๋Ÿฌ๋‚˜ ๋ฐฐ์น˜ ์— ์žˆ์–ด ์„œ ๋Š” ์ด๋Ÿฐ ํ‹€ ์ด ํฌ๊ฒŒ ๋‹ค๋ฅด๋‹ค.
    ์˜ˆ ๋ฅผ ๋“ค ์–ด springboot maven ํ”Œ๋Ÿฌ๊ทธ ์ธ ์€ Maven ์—์„œ springboot ์ง€์› ์„ ์ œ๊ณต ํ•ฉ ๋‹ˆ ๋‹ค.jar ๋‚˜ war ๋ฅผ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ ๋„๋ก ํฌ์žฅ ํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ ์„'ํ˜„์ง€์—์„œ'์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ ์Šต ๋‹ˆ ๋‹ค.
    ๋ฐฐ์น˜ ํ™˜๊ฒฝ ์—์„œ Spring Boot ๊ฐ€ Spring ๋ณด๋‹ค ์ข‹ ์€ ์žฅ์  ์€ ๋‹ค์Œ ๊ณผ ๊ฐ™๋‹ค.
  • ๋‚ด์žฅ ์šฉ๊ธฐ ์ง€์›
  • ๋ช…๋ น ์ž๋ฐ”-jar ๋ฅผ ์‚ฌ์šฉ ํ•˜์—ฌ jar ๋ฅผ ๋…๋ฆฝ ์  ์œผ๋กœ ์‹คํ–‰ ํ•˜๋„๋ก ์„ค์ •
  • ์˜์กด ํ•ญ๋ชฉ ์„ ์ œ๊ฑฐ ํ•˜ ๋Š” ์˜ต์…˜ ์œผ๋กœ ์™ธ๋ถ€ ์šฉ๊ธฐ ์— ๋ฐฐ์น˜ ํ•  ๋•Œ ์ž ์žฌ ์  ์ธ jar ์ถฉ๋Œ ์ด ๋ฐœ์ƒ ํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ ๋‹ˆ ๋‹ค
  • ๋ฐฐ์น˜ ํ•  ๋•Œ ํ™œ์„ฑ ํ”„๋กœํ•„ ์„ ์ง€์ • ํ•˜ ๋Š” ์˜ต์…˜
  • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์˜ ๋žœ ๋ค ํฌํŠธ ์ƒ ์„ฑ
  • ๊ฒฐ๋ก .
    ๋ณธ ๋…ผ๋ฌธ ์—์„œ ์šฐ ๋ฆฌ ๋Š” Spring ๊ณผ Spring Boot ๊ฐ„ ์˜ ์ฐจ ์ด ๋ฅผ ์ดํ•ด ํ–ˆ๋‹ค.
    ์‰ฝ๊ฒŒ ๋ง ํ•˜๋ฉด spring boot ๋Š” Spring ์ž์ฒด ์˜ ํ™•์žฅ ์ผ ๋ฟ ๊ฐœ๋ฐœ,ํ…Œ์ŠคํŠธ ์™€ ๋ฐฐ ์น˜ ๋ฅผ ๋”์šฑ ํŽธ๋ฆฌ ํ•˜ ๊ฒŒ ํ•œ๋‹ค ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
    ์ด์ƒ ์€ Spring ๊ณผ SpringBoot ๊ฐ„ ์˜ ์ฐจ์ด ์— ๋Œ€ํ•œ ์ƒ์„ธ ํ•œ ๋‚ด์šฉ ์ž… ๋‹ˆ ๋‹ค.Spring ๊ณผ SpringBoot ์˜ ์ฐจ์ด ์— ๊ด€ ํ•œ ์ž ๋ฃŒ ๋Š” ๋‹ค๋ฅธ ๊ด€๋ จ ๊ธ€ ์„ ์ฃผ๋ชฉ ํ•˜ ์„ธ ์š”!

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ