개발 환경 설정 및 툴 사용법

스프링 부트 #1 - 설정 및 테스트 ( 제작 중 )

letsDoDev 2023. 9. 24. 14:14

<<< 스프링 부트를 이용한 수동 웹 어플리케이션 생성 방법 >>>

스프링 부트를 위한

- pom.xml

 	<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
     http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mastering.spring</groupId>
	<artifactId>springboot-example-ch5</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>First Spring Boot Example</name>
	<packaging>war</packaging>
	<!-- 스프링 부트 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
	</parent>
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-rest-hal-browser</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<!-- <scope>test</scope> -->
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<!-- <scope>provided</scope> -->
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>

- application.java

 	package com.mastering.spring.springboot;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);
		
		/*
		String[] beanNames = ctx.getBeanDefinitionNames();
		Arrays.sort(beanNames);
		for (String beanName : beanNames) {
			System.out.println(beanName);
		*/
		
		/*
		 // Applcation.java의 역할
		 1. 스프링의 ApplicationContext 인스턴스 생성
		 2. 인수 허용 및 스프링 속성으로 표시
		 3. 설정되어 있는 모든 빈을 로드한다. ( 스캔 경로 지정: X)
		 - 클래스의 정적 run 메소드 사용
		 */	
			
		}
	}
}

[@SpringBootApplication 요구사항]

@Configuration | 스프링 어플리케이션 컨텍스트 구성 파일임을 표시

@EnableAutoConfiguration |  자동 설정 기능 활성화

@ConponentScan | 클래스의 패키지와 모든 하위 패키지에서 스프링 빈을 스캔한다.

 

- java 로 실행시킨 결과

※ 유의할 점

Spring Boot 에 내포되어 있는 톰캣이 실행되므로 기존에 apache tomcat이  설치 실행 되어있다면
실행을 멈춰둔 후 실행시킨다.

그럼에도 불구에고 이미 사용 중인 포트라고 뜰 경우 cmd 로 해당 포트를 죽인 후 다시 실행시키거나
resource/application.properties 파일에 server.port = 대체해서 사용할 포트 번호를 기입 후 다시 실행시킨다
spring boot의 기본 포트 번호틑 8080이다

- application.java 파일에 주석체크 되어있는 코드를 해제한 후 실행시키면
  Springboot 실행과 함께 의존 관계에 있는 bean까지 확인가능하다.