목표
Jira : https://spring.io/blog/2019/01/15/spring-framework-s-migration-from-jira-to-github-issues(Spring Boot 프로젝트 시작)
Spring Boot 1.5.x Relase(2017.01)
Java8이상
Spring Framework 4.3
@Valid -> JSR303
Spring Boot : HikariCp (그 전까지는 Apache Common DBCP 사용했엇음)
Graceful Shutdown : Application을 종료 할 예정. 그렇다면 새로운 Connection 생성 X, 하지만 지금까지 받은 Connection은 성공적으로 완료하고 종료할것임. -> Thread Pool에 Active Thread가 없으면 종료
Docker Image Building 지원(Jar,War)
Java16의 Record를 @ConfigurationProperties로 사용가능
Record : 클래스같이 쓸 수 있지만, ValueObject를 위해 쓸 수 있는 키워드
@ConfigurationProperties : Properties와 비슷하지만, type-safe하게 쓸 수 있게 해줌
!! 순환참조 Bean은 금지가 됨(Spring Boot2.6)
- spring.main.allow-circular-references
- Bean A -> Bean B -> Bean C -> Bean A
Auto configuration 파일 위치 변경(Spring Boot 2.7)
Spring Boot 3
Java17지원, Spring Framework 6
Spring Boot Release : 지원기간 정책 제공
OSS support - 보안 업데이트와 버그 수정 무료 지원
Commercial support - OSS EOL 이후 스프링 전문가로부터 commercial support를 구매 할 수 있음
Spring Boot 목표
1) Java -jar로 실행 가능(Tomcat을 다운받고, Tomcat이 war를 구동시키도록(war/war-exploded 한것이 기존의 방식)
- Jar, War / 한번 다시 읽고오기
2)
Spring Boot의 기능
Spring Core(Framework과 비교)
라이브러리 의존성을 pom.xml 직접 설정해야 함(Core)
- spring boot에서는 spring-boot-statrter-{module}만 설정하면 필요 라이브러리 설정 완료(Boot)
버전 정보를 직접 설정 및 테스트 해야함
- Spring - boot - starter - parent에서 spring module의 버전 및 써드파티 라이브러리 버전 제공
런타임에만 확인 가능한 귀찮은 작업
WAS에 배포해야 함
- 내장형 WAS를 제공해서 서버를 구매/설정할 필요가 없음(공공 서버는 WAS 구매함)
SpringBoot에서 MySQl을 연결하고 싶을 때,
1) Dependency 추가
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
2) application.properties 수정
!!!현업에서는 쓰면 안됨!
spring.jpa.generate-ddl=true
학습용 로깅 설정
spring.jpa.show-sql=true
datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student_test?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.datasource.username=root
spring.datasource.password=test
Initialize and Run Spring-Boot project
intellij Ultimate에서는 SpringBootInitializer를 자체 지원한다
Excutable Jar/War -> 이미 톰캣이 내장되있어서 JAVA만 있으면 됨!
Build Tool -> Maven/Gradle
Unix/Linux Services
- init.d Service
- systemd Service
Docker/Kubernetes -> Docker Image 생성 지원
Spring Web Application 만들기(Boot에서)
Boot가 지원하는 템플릿 : Thymeleaf(기본-html 문법 내에서 view를 구현 가능), FreeMarker, Mustache, Groovy Templates ,Velocity
타임리프를 기본 제공하니 의존성을 추가해야 한다;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
src/main/resource/dir/foo.html << 이렇게 작성 dir은 주로 template이라는 명을 쓴다
View 사용 -> @Controller
타임리프 사용 -> Application properties에 사용설정 해야함
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates(resources하위폴더)/main/
spring.thymeleaf.suffix=.html
Boot는 JSP를 권장하지는 않는다...하지만 Legacy의 코드를 재사용 하기 위해 학습할 필요가 있다
Spring-Boot에서 JSP 제약
```xml
<packaging>war</packaging>
...
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
```War로 패키징 한 경우 Main 클래스가 SpringBootInitializer를 상속받아야 한다.
@SpringBootApplication
public class StudentApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(StudentApplication.class);
}
}
그 후 application.properties에 prefix, suffix를 달아줘야 한다
Dependency Mgnt and Main Function
spring-boot-starter
spring-boot-starter-parent
spring-boot-dependencies - 사용하는 라이브러리 버전을 property로 관리
<properties>
<activemq.version>5.16.5</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.98</appengine-sdk.version>
<artemis.version>2.19.1</artemis.version>
<aspectj.version>1.9.7</aspectj.version>
<assertj.version>3.22.0</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<awaitility.version>4.2.0</awaitility.version>
</properties>
spring-boot-starter-web:spring-core, spring-web, spring-webmvc(interceptor 포함), 내장 tomcat server 포함
<dependencymanagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-web</artifactId>
<version>2.7.6</version>
</dependency>
...
</dependencies>
</dependencymanagement>
Main Class Function
Spring-boot 프로젝트의 main 메소드
SpringApplication 사용방법
static method (가장 일반적인 사용방법)
use construction(static 메소드 내부에 동일한 구현이 있다)
use builder(builder로 여러 web-context를 구성할 수 있으며, parent-child의 계층구조로 설정할 수 있다)
Spring Banner를 끄고 싶다면....
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}
Spring Banner를 바꾸고 싶다면...
Banner에 사용할 수 있는 변수. banner.txt에는 다음의 변수들을 쓸 수 있다