인프런/스프링 부트 개념과 활용

2일차 강의

곰돌이볼 2023. 6. 14. 14:13

 

  • 오늘 공부 분량 
    • 3부 : 스프링 부트 원리(자동 설정 이해) ~ 내장 웹 서버 응용 1부 : 컨테이너와 포트
    • 자동 설정 만들기 1, 2부 -> 실습해보면 좋을듯

 

 

공부 요약


  • main 메서드에서 @SpringBootApplication 애너테이션만으로 서버가 동작하는 이유
    • 아래 3개의 애너테이션이 @SpringBootApplication과 동일함 
    • @SpringBootConfiguration
    • @ComponentScan
    • @EnableAutoConfiguration
  • SpringApplication은 빈을 2번 등록함
    • @ComponentScan
    • @EnableAutoConfiguration
    • 위 애너테이션이 역할을 함

 

    •  @SpringBootApplication 애너테이션 없이 커스텀한 자동설정을 통해서 코드 실행 가능
      • 단, 웹 서버로 동작하지 않음
@Configuration
@ComponentScan
public class Application {
	public static void main(String[] args) {
    	SpringApplication application = new SpringApplication(Application.class);
        application.setWebApplicationType(WebApplicationType.NONE);
        application.run(args);
    }
}

 

  • @ComponentScan
    • 현재 디렉터리를 포함한 하위 디렉터리를 탐색해서 @Component 애너테이션을 가진 클래스를 bean으로 등록
    • @Component 애너테이션 : @Component, @Configuration, @Repository, @Service, @Controller, @RestController 등등
  • @EnableAutoConfiguration
    • meta 파일(META-INF의 spring.factories 내용)을 읽어옴
    • @ConditionalOn... 와 같이 되어 있는건 조건에 따라서 bean 등록 여부 결정

 

  • 자동설정
    • xxx-Spring-Boot-Autoconfigure : 자동 설정
    • xxx-Spring-Boot-Starter 모듈 : 필요한 의존성 정의
    • 아래의 코드의 의존성 추가하기
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-autoconfigure</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-autoconfigure-processor</artifactId>
		<optional>true</optional>
	</dependency>
</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.0.3.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
  • 과정(xxx-Spring-Boot-Starter 모듈 정의해서 다른 프로젝트해서 활용하기)
    • @Configuration 파일 작성
    • src/main/resource/META-INF 위치에 spring.factories 파일 생성
    • spring.factories 파일에 자동 설정 파일 내용 추가하기
    • mvn install
  • @ConditionalOnMissingBean
    • 새로운 Bean을 생성했을 때, 이전에 등록된 bean이 등록되지 않도록 방지

 

  • 내장 서블릿 컨테이너
    • spring boot는 서버X
      • 톰캣을 통해서 서버를 운영
    • 톰캣 설정, 포트 설정, 서블릿 생성, 톰캣 실행 등의 과정을 실행하는 것이 스프링 부트의 자동 설정
      • 이를 유연하게 실행하는 것이 스프링 부트의 자동 설정
      • ServletWebServerFactoryAutoConfiguration : 서블렛 웹 서버 생성
      • TomcatServletWebServerFactoryCustomizer :  서버 커스터마이징
      • DispatcherServletAutoConfiguration : 서블릿 생성 및 등록

 

  • 내장 서블릿 컨테이너 응용
    • 랜덤 포트 : yml 파일에 (server:port:0) 하면 사용 가능한 포트로 서버 실행

 

 

 

'인프런 > 스프링 부트 개념과 활용' 카테고리의 다른 글

4일차 강의  (0) 2023.06.20
3일차 강의  (0) 2023.06.19
1일차 강의  (0) 2023.03.17