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

3일차 강의

곰돌이볼 2023. 6. 19. 14:13
  • 오늘 공부 분량
    • 3부 내장 웹 서버 응용 2부 : HTTPS와 HTTP2 ~ 4부 프로파일
    • 내장 웹 서버 응용 2부 : HTTPS와 HTTP2  → HTTPS2 설정할 떄 보면 좋을 듯

 

 

공부 요약


  • HTTPS 설정하기
    • 키스토어 만들기 -> 커넥터가 하나이기 때문에 HTTP와 HTTPS 동시 사용 X
    • HTTP 커넥터는 코딩으로 설정하기
      • https://github.com/spring-projects/spring-boot/tree/v2.0.3.RELEASE/spring-boot-samples/spring-boot-sample-tomcat-multi-connectors
      • 위 코드를 참고해서 serverFactory와 Connect 메서드 선언하기
    • HTTP2 설정
      • properties 추가하기 : server.http2.enable=true
      • 사용하는 서블릿 컨테이너 마다 다름
      • 사용하기 위해서 SSL 설정이 되어있어야 함
  • JAR 파일
    • jar 파일만으로 코드 실행 가능
    • 콘솔창에서 mvn package 입력 -> 실행 가능한 JAR 파일 생성
    • spring-maven-plugin이 해주는 일 : 패키징
    • 과거 “uber” jar 를 사용
      • 모든 클래스 (의존성 및 애플리케이션)를 하나로 압축하는 방법
      • 모든 파일이 하나의 방법으로 압축되어서 어떤 라이브러리를 사용하는지 알 수 없음
    • 스프링 부트의 전략
      • 내장 JAR -> 기본적으로 자바에는 내장 JAR를 로딩하는 표준적인 방법 X
      • 애플리케이션 클래스와 라이브러리 위치 구분
      • org.springframework.boot.loader.jar.JarFile 사용해서 내장 JAR 읽기
      • org.springframework.boot.loader.Launcher 사용해서 실행

 

  • SpringApplication
    • 커스텀
@SpringBootApplication
public class SpringbootgettingstartedApplication {

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

        // 위의 코드와 동일한 기능을 함(커스텀하기 위해서 아래와 같이 선언함)
//        SpringApplication app = new SpringApplication(SpringbootgettingstartedApplication.class);
//        app.run(args);
    }
}

 

  • 배너 설정하기 1)
    • source 패키지에 banner.txt 생성 후 원하는 것으로 커스텀 가능함

  • 배너 설정하기 2)
    • main application에 코드 추가하기
@SpringBootApplication
public class SpringbootgettingstartedApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringbootgettingstartedApplication.class);

        // banner 설정
        app.setBanner(new Banner() {
            @Override
            public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
                out.println("------------------------");
                out.println("banner ~~");
                out.println("------------------------");
            }
        });

        app.run(args);
    }
}

 

  • 배너 설정하기 3)
    • builder 사용하기
@SpringBootApplication
public class SpringbootgettingstartedApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .sources(SpringbootgettingstartedApplication.class)
                .banner(new Banner() {
                    @Override
                    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
                        out.println("------------------------");
                        out.println("banner ~~");
                        out.println("------------------------");
                    }
                }).run(args);
    }
}

 

  • properties 우선순위
    • 1. 유저 홈 디렉토리에 있는 spring-boot-dev-tools.properties
      2. 테스트에 있는 @TestPropertySource
      3. @SpringBootTest 애노테이션의 properties 애트리뷰트
      4. 커맨드 라인 아규먼트
      5. SPRING_APPLICATION_JSON (환경 변수 또는 시스템 프로티) 에 들어있는
      프로퍼티
      6. ServletConfig 파라미터
      7. ServletContext 파라미터
      8. java:comp/env JNDI 애트리뷰트
      9. System.getProperties() 자바 시스템 프로퍼티
      10. OS 환경 변수
      11. RandomValuePropertySource
      12. JAR 밖에 있는 특정 프로파일용 application properties
      13. JAR 안에 있는 특정 프로파일용 application properties
      14. JAR 밖에 있는 application properties
      15. JAR 안에 있는 application properties
      16. @PropertySource
      17. 기본 프로퍼티 (SpringApplication.setDefaultProperties)

 

  • application.properties 우선 순위(파일 위치에 따른)
    1. file:./config/
    2. file:./
    3. classpath:/config/
    4. classpath:/

 

  • properties에서 랜덤값 설정하기
    • 환경변수 : ${random.*}
  • 서버 포트번호를 랜덤으로 사용
    • server.port = 0
    • 사용 가능한 서버 포트 중 하나를 랜덤으로 생성

 

  • 여러 개의 properties 사용하기
    • 클래스 선언
    • 애너테이션 : @Component, @ConfigurationProperties("프로퍼티에서 사용하는 변수")
    • 융통성 있는 바인딩 -> 카멜, 스네이크, 대문자로 이름 형식 달라도 융통성 있게 프로퍼티와 클래스를 연결함

 

  • @Profile
    • 특정 properties에서만 동작하는 클래스를 선언하고 싶을 때 사용하는 애너테이션
    • 클래스 위에 @Profile("properties에 추가로 작성하는 것")

 

 

 

 

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

4일차 강의  (0) 2023.06.20
2일차 강의  (0) 2023.06.14
1일차 강의  (0) 2023.03.17