본문 바로가기

프로그래밍/JAVA & SPRING

[LifeSoft] spring 25강 Spring Boot와 Oracle 연동, Thymeleaf Template 적용

반응형

15. spring boot

가. spring boot

2014년부터 개발되었으며 spring legacy project에 비해서 설정이 매우 간소화됨

WAS(tomcat)가 포함되어 있으므로 서버 설정이 간소화됨

아직 실무에서 많이 사용되지 않고 있지만 향후 spring legacy project를 대체하리라 예상됨

 

1) Spring Starter Project 생성

Name : 프로젝트 이름(spring03_boot)

Artifact : spring03_boot

Package : com.example.spring03

 

New Spring Starter Project Dependencies 창에서

Boot Version : 최신 버전 2.1.4

SQL : MySQL, JDBC Mybatis 체크

Template Engines : Thymeleaf 체크

Web : Web 체크

 

다음 누르면

Base Url : https://start.spring.io/starter.zip

Full Url  : https://start.spring.io/starter.zip?name=spring03_boot&groupId=com.example&artifactId=spring03_boot&version=0.0.1-SNAPSHOT&description=Demo+project+for+Spring+Boot&packageName=com.example.spring03&type=maven-project&packaging=jar&javaVersion=1.8&language=java&bootVersion=2.1.4.RELEASE&dependencies=mysql&dependencies=mybatis&dependencies=jdbc&dependencies=thymeleaf&dependencies=web

그대로 사용한다.

Finish하면 관련 라이브러리를 다운받고 프로젝트가 구성이 된다.

구성되는 동안 프로젝트는 close 상태로 있다 (이거 좋은거 같음^^)

 

디렉토리 구조가 이전과는 많이 다르다.

pom.xml을 보면

스프링프레임워크도 최신버전으로 세팅되어 있다.

설정이 많이 숨겨져 있다.

legacy project

 

 <properties>
  <java-version>1.8</java-version>
 
  <org.springframework-version>5.1.0.RELEASE</org.springframework-version>
  <org.aspectj-version>1.9.2</org.aspectj-version>
  <org.slf4j-version>1.7.25</org.slf4j-version>
 </properties>

 

 

spring boot project

pom.xml 설정이 간단해졌다.

 

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/>
 </parent>
 <properties>
  <java.version>1.8</java.version>
 </properties>
<dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>2.0.1</version>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

 

 

com.example.spring03.Spring03BootApplication.java를 보면

main() 메소드가 나온다. 이게 시작 클래스다.

이 메소드를 실행시키면 프로그램이 돌아간다.

실행버튼 누르고 Spring Boot App으로 실행해야 함(Java Application 아님)

 

로고를 보면 스프링 로고와 버전이 찍혀있다.

처음엔 에러가 난다 (처음 설치를 해서이다)

 

톰캣이 내장되어 있다. 로고를 보면

main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

WebApplicationContext

classpath:/templates/  이런 것들이 보인다.

 

-------------------------------

APPLICATION FAILED TO START

-------------------------------

Reason: Failed to determine a suitable driver class

아직 세팅이 다 되지 않아 에러가 나서 멈춰있는 상태다.

database driver 설정이 안되어 있다.

 

 

2) Spring boot 프로젝트의 실행 방법

시작 클래스 : 프로젝트이름 + Application.java

tomcat이 내장되어 있음. 별도로 설정할 필요 없다.

Run as Spring Boot App

기존에 실행중인 톰캣은 중지시키고 실행시켜야 함.

 

 

3) 템플릿 엔진

spring boot application에서는 화면 출력을 담당하는 뷰로 jsp대신 template을 사용하는 것을 권장하고 있음.

spring boot 에서 사용가능한 template 에는 여러 종류가 있는데 그중에 타임리프(Thyme leaf)를 활용하여 실습

thyme - 백리향