본문으로 바로가기

[Spring] Spring vs Spring Boot

category Web/Spring, JPA 2022. 8. 30. 13:02

https://jmxx219.tistory.com/28?category=1011187 

 

[Spring] 스프링(Spring)이란?

스프링 스프링 DI 컨테이너 기술 스프링 프레임워크 스프링 부트, 스프링 프레임워크 등을 모두 포함한 스프링 생태계 스프링 생태계 스프링 프레임 워크 핵심 기술: 스프링 DI 컨테이너, AOP, 이

jmxx219.tistory.com

Spring과 Spring Boot의 차이점에 대해 정리해보고자 한다.

Spring

1. DI 의존성 주입

2. 중복된 코드 제거

3. 다른 프레임워크와의 통합

 

 

Spring Boot

스프링을 편리하게 사용할 수 있도록 지원, 최근에는 기본으로 사용(개발자가 개발에만 더 집중할 수 있도록!!)

 

 

1. Embedded Server(Embedded Servlet Container)

 

단독으로 실행할 수 있는 스프링 애플리케이션을 쉽게 생성

 

  • Tomcat 같은 웹 서버를 내장 -> 별도의 웹 서버를 설치 X
  • 애플리케이션 배포 단순
    • Spring과 Spring Boot 모두 Maven 또는 Gradle과 같은 패키징 기술을 지원 
    • Spring은 war(Web Application Archive) 파일을 WAS에 담아서 배포
    • Spring Boot는 내장 웹 서버를 가지고 있어 jar 파일로 배포 가능
      • java -jar 명령을 이용하여 실행가능한 jar 아카이브를 패키징하여 독립적으로 실행 가능

 

2. Dependency 자동화

 

손쉬운 빌드 구성을 위한 Spring-boot-starter 종속성 제공

 

1) Spring Freamework

<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의 최소 종속성
  • 이외에도 웹 애플리케이션을 개발할 때 많은 의존성이 필요
  • 호환되는 버전에 맞춰 의존성을 추가하기 어려움

 

2) Spring Boot

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.4</version>
</dependency>
  • Spring과 달리 Spring Boot는 웹 애플리케이션을 생성하기 위해 하나의 종속성만 필요
  • 다른 모든 종속성은 빌드 시간동안 최종 아카이브에 자동 추가
  • Spring에 대한 다양한 starter 종속성 제공
    • spring-boot-starter-data-jpa
    • spring-boot-starter-security
    • spring-boot-starter-test
    • spring-boot-starter-web
    • spring-boot-starter-thymeleaf ...

 

3. Auto Configuration

 

1) Spring 기능을 위한 자동 설정 지원

 

@SpringBootApplication

@SpringBootApplication
public class TestWebApplicaton {
	public static void main(String[] args) {
    	SpringApplication.run(TestWebApplicaton.class, args);
    }
}
  •  Spring Boot 프로젝트의 main 메소드
@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 {
}
  • @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
  • Spring으로 설정을 한다면 bean과 설정 파일을 모두 등록해주어야함
  • Spring Boot의 자동 설정 기능을 이용하여 bean을 자동으로 등록하고 설정할 수 있음

 

@SpringBootConfiguration

  • Spring Boot 환경설정을 나타내는 어노테이션
  • @Configuration과 동일한 역할
  • 한 서비스 내에 해당 어노테이션을 2개 이상 사용 불가능 -> 구성을 자동으로 찾을 수 있음

 

@EnableAutoConfiguration

  • @ComponentScan으로 빈이 등록된 후에 추가적인 빈을 읽어 등록하는 어노테이션
  • spring.factories 안에 있는 자동 설정들의 조건에 따라 Bean이 생성되고 자동 설정됨
  • META-INF/spring.factories : 자동 설정 타깃 클래스 목록 선언

 

@ComponentScan

  • @component 어노테이션을 선언한 클래스를 찾고 Bean을 생성함
  • @Controller, @Service, @Repository ... 등등

 

2) starter 의존성을 통한 간단한 설정

 

1) Spring Freamework

 

  • 웹 애플리케이션 생성 시 웹과 관련된 여러 설정 파일을 직접 생성 및 관리
    • applicationContext.xml : 스프링 빈 설정 파일
    • web.xml : 웹 애플리케이션 설정을 위한 배포와 관련된 설정 파일

 

2) Spring Boot

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.1.RELEASE'
  • starter를 이용하여 관련 의존성 추가 및 모듈에 맞게 자동으로 설정 가능
  • Gradle에 starter 의존성을 추가하면 관련 모듈(jar)도 함께 자동으로 classpath에 추가됨
  • classpath에 추가된 jar 파일들을 바탕으로 spring.factories 파일에 있는 관련 설정이 자동으로 수행

 


+ Spring Initializr : https://start.spring.io/ 

 

 

참고

https://www.baeldung.com/spring-vs-spring-boot

https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.auto-configuration