Spring

[Spring] 나만의 MVC패턴 만들기 - 1 (개발환경세팅)

inderrom 2023. 11. 13. 13:54

1.버전

- java 11 (temurin)

- apach-tomcat 9.0

 

2. IDE

- IntelliJ

 

3. Gradle설정을 해주어야 스프링 프로젝트로 생성 가능하다.

 

 

 

 

4. build.gradle 

  • 아래 코드로 dependencies 설정을 해준 후 import

 

dependencies {

    implementation 'org.apache.tomcat.embed:tomcat-embed-core:8.5.86'
    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:8.5.86'

    implementation 'javax.servlet:javax.servlet-api:4.0.1'
    implementation 'javax.servlet:jstl:1.2'

    implementation 'ch.qos.logback:logback-classic:1.2.9'


    testImplementation 'org.junit:junit-bom:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

 

 

5. tomcat 실행 소스 작성

  • main 클래스(이름변경:WebApplicationServer)에 소스 작성 후 import 
  • logger -> org.slf4j.java로 import
  • 이대로 실행시키는 경우 comfile error 발생

 

package org.example;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class WebApplicationServer {

    private static final Logger log = LoggerFactory.getLogger(WebApplicationServer.class);


    public static void main(String[] args) throws LifecycleException {
        String webappDirLocation = "webapps/";
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);

        tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        log.info("configuring app with basedir: {}", new File("./" + webappDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();
        

    }
}

 

 

 6. project settings, preferences

  • webapps/WEB-INF 경로로 실행해야함
  • project settings -> Modules -> main - path에서 경로 변경 , test도 똑같이 경로 변경

  • preferences -> Build, Execution, Deploymet->Gradle -> Gradle Projects
  • Build and run using, Run tests using -> IntelliJ IDEA로 변경

  • 실행시 webapps/WEB-INF 안에 파일 생성됨

 

 

7. 도커 설정

 

Docker Desktop: The #1 Containerization Tool for Developers | Docker

Docker Desktop is collaborative containerization software for developers. Get started and download Docker Desktop today on Mac, Windows, or Linux.

www.docker.com

  • docker -v 로 설치 확인
  • MySQL 도커 컨테이너 생성 및 실행
    • docker run --name mysql-sample-container -e MYSQL_ROOT_PASSWORD=<password> -d -p 3306:3306 mysql: {version}
  • 실행중인 도커 컨테이너 목록 출력
    • docker ps -a
  • MySQL 도커 컨테이너 접속
    • docker exec -it {도커 컨테이너 이름} bash

  • MySQL 접속
    • mysql -u root -p