본문 바로가기

웹개발 풀스택 과정/Spring Legacy

36일차(2022.02.15) _ Spring Project 생성 후 기본 설정

1. pom.xml

 

	<properties>
    	<!-- 다음과 같이 수정 -->
		<java-version>1.11</java-version>
		<org.springframework-version>4.3.2.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
	</properties>
	...
	...
	<dependencies>
		<!-- 라이브러리 관리, 추가, 수정, 삭제 -->
		<!-- jdbc -->
		<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
		<dependency>
		    <groupId>com.oracle.database.jdbc</groupId>
		    <artifactId>ojdbc8</artifactId>
		    <version>19.8.0.0</version>
		</dependency>	
	...
	...
		<!-- Test -->
		<!-- Spring과 Junit 연결하는 라이브러리 추가 -->
		<!-- 현재 Spring과 동일한 버전을 추가 (4.3.2) -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-test</artifactId>
			<!-- ${org.springframework-version} 표현식으로 4.3.2 변수를 가져옴 -->
		    <version>${org.springframework-version}</version>  
		    <scope>test</scope>
		</dependency>
        
		<!-- Test 프로그램 junit 버전 수정 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

 

properties 는 pom.xml 에서 사용하려는 변수들이 들어가있다.

properties에서 정의를 하면, 아래에서는 변수명만 입력하면 값들을 대입할 수가 있다.

같은말로, properties만 수정하면 아래에 대입한 값들이 전부 한꺼번에 수정이 된다는 소리이다.

 


2. web.xml

 

	<filter>
		<filter-name>encode</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>encode</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

web.xml 상단 web-app 태그 다음부터 위의 코드를 작성

한글 인코딩을 처리하는 코드이다.

 


3. Junit Test 도구 준비

 

- Junit Test 도구는 Back-end(java)를 서버실행과 클라이언트 요청없이 테스트할 수 있음

- src/test/java 내부의 비어있는 베이스 패키지 내에 Test case 생성
- root-context와 servlet-context를 참조할 수 있게 어노테이션 작성
- 해당 클래스를 추상클래스로 만듬
- 테스트케이스를 만들 때마다 해당 클래스를 상속

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/*-context.xml"})

 

클라이언트가 요청이 들어오면 서버에서는 배포서술자인 web.xml을 참조해서

스프링 문서를 참조하게 된다.

잠깐 web.xml문서를 살펴보자

 

 

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>

	<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>

 

이렇게 web.xml에서도 context-param, servlet 태그로 객체를 만든다.

그리고 파라미터를 만들어서 각각 root-context와 servlet-context 파일의 위치가 어디인지 알려주고 있다.

그래서 클라이언트에서 요청이 들어오면 자동으로 이 두 개의 파일들을 참조해서 

우리가 설정한 객체를 만들어주거나 파일의 경로를 참조하게 되는 것이다.

 

근데 TestCase의 경우에는 요청없이, 서버실행 없이 Test를 하는 도구이다.

즉, web.xml이 실행되지 않는다.

이 말은 TestCase를 실행하면 자동으로 root-context와 servlet-context를 참조하지 못하기때문에

TestCase를 만들 때마다 위의 어노테이션 2개를 추가를 해줘야 한다.

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/*-context.xml"})
public abstract class MyJunitTest {

}

 

차라리 그럴바에는, 이렇게 추상클래스를 하나 만들어 놓은 다음에

다음에 테스트케이스를 하나 추가할 때마다 이 클래스를 상속받으면 된다.