organize/스프링

spring(스프링) 프로젝트 생성

001cloudid 2024. 2. 3. 16:21
728x90
더보기

글 작성 이유

 

스프링으로 팀 프로젝트를 진행하는데 있어서 초기 설정부터 어려움에 부딪혔다.

그래서 팀 프로젝트를 원활하게 진행 할 수 있도록 아주 기초적인 부분부터 정리하기 위해 작성한다.

스프링 프로젝트 생성

1. 프로젝트 생성

 

2. 생성된 프로젝트 최상위 단에서 Alt + Enter(Properties)

 1) Java Compiler - Use compliance from execution environment 'JavaSE - 11' on the 'Java Build Path' 체크 해제 후

      11으로 변경

 

 2) Project Facets - Dynamic Web Module 3.1, Java 11으로 변경

 

1), 2) 설정이 완료되었으면 Apply and Close - Yes

 

스프링 프로젝트 설정

사람마다 정리하는 방식이 조금씩 다르겠지만, 나는 그냥 아래에서부터 위로 조금씩 수정하는 것을 선호하기 때문에

pom.xml부터 시작한다.

 

1. pom.xml

Maven의 빌드 정보를 담고 있는 파일. POM(Project Object Model)을 설정하는 부분으로 프로젝트 내 빌드 옵션을 설정하는 부분이다. 이곳에 https://mvnrepository.com에서 원하는 프로그램 코드를 복사해서 붙혀넣으면 Maven Dependencies에 자동으로 다운받아진다.

 

pom.xml을 열어보면 각각의 태그에 정리

<project> : Maven의 XML 네임스페이스를 지정
<modelVersion> : Maven의 model Version
<groupId> : 그룹 ID 태그
<artifactId> : 아티팩트 ID 태그
<version> : 버전명 태그
<packaging> : 패키징 형식을 지정하는 태그
<name> : 프로젝트의 이름
<url> : Maven의 URL
<properties> : 프로젝트 관련 속성
<parent> : pom.xml의 상속에 관련된 태그
<dependencies> : 프로젝트가 의존하는 라이브러리들의 정보

 

dependency 태그 안의 태그 정리

<groupId>라이브러리의 그룹아이디</groupId>
<artifactId>라이브러리의 아티팩트아이디</artifactId>
<version>라이브러리 버전</version>
<scope>라이브러리를 사용할 범위</scope>

 

팀프로젝트를 하면서 사용할 pom.xml은 아래와 같다.

(...생략...)
	<properties>
		<java-version>11</java-version>
		<org.springframework-version>4.3.8.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
	</properties>
	<dependencies>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<version>8.0.33</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>

		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>

		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<additionalProjectnatures>
						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
					</additionalProjectnatures>
					<additionalBuildcommands>
						<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
					</additionalBuildcommands>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.2.1</version>
				<configuration>
					<mainClass>org.test.int1.Main</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 

사실 전부 알지 못한다. 그냥 자바 버전, 스프링버전 변경, mysql, mybatis 등을 추가함.

 

2. web.xml

웹 애플리케이션을 실행 시킬 때 함께 올라가야할 설정들을 정의해 놓는 곳

Deployment Descriptor(배포 설명자)라고 불림.

DispatcherServlet(클라이언트의 요청 처리), ContextLoaderListener(웹 애플리케이션 컨텍스트 단위로 설정 로드), Filter를 설정

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
<!-- post전송 request 한글처리 "utf-8" 설정 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

 

3. root-context.xml

web.xml 파일에서 가장 먼저 읽어 들이는 설정 파일로 모든 서블릿이 공유할 수 있는 Bean들이 모인 공간으로 공통 bean을 설정.

프로젝트 전반으로 사용되는 자원을 생성하고 설정하는 파일

View와 밀접하지 않은 정보를 정의하는 곳이며, DB 연결 관련 정보, 트랜잭션 처리, 파일 업로드 등을 작성한다.

 

4. servlet-context.xml

web.xml에서 DispatcherServlet의 설정을 기록하는 파일 root-context.xml과 달리 개별적인 Bean을 설정
요청과 관련된 객체를 정의(어노테이션, url)

<annotation-driven / > : 어노테이션 사용을 가능하게 함
<resources> : 정적인 리소스들의 정보를 기술함. 예를 들어 이미지, 스타일시트, 자바스크립트 파일 위치
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> : 서블릿 설정으로, 접두사와 접미사를 붙여서 경로 설정
<context:component-scan> : 해당 패키지에 있는 파일들의 어노테이션을 스캔해서 Bean으로 등록하는 역할

즉, <context:component-scan>을 통해 패키지 폴더를 <annotation-driven / >으로 어노테이션을 검사한 후 어노테이션 동작

 

 

 

728x90

'organize > 스프링' 카테고리의 다른 글

프레임워크, Spring과 Spring Boot  (1) 2024.10.04
redirect, redirect와 RequestDispatcher  (0) 2024.10.03
페이지 처리  (0) 2024.10.02
스프링 프로젝트 정리 1  (0) 2024.09.18
1. 개발을 위한 준비  (0) 2024.08.27