목표:스프링의 의존성을 설정하고 테스트해본다.
https://lee-coding.tistory.com/9?category=1050147
스프링 환경 설정
목표:스프링을 공부하기 위한 환경을 설정한다 미리 설치한 프로그램: eclipse 2022-06 버전, 자바 11버전, apache-tomcat-9.0.67 1. eclipse 2022-06 버전을 사용한다. 2. 이클립스 설치 폴더 안에 eclipse.i..
lee-coding.tistory.com
위와 동일하게 설정하고 Spring MVC Project의 이름을 Spring_AtoZ로 설정하여 프로젝트를 생성하였다.
pom.xml에 다음 코드를 넣는다.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
pom.xml 코드 전문
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.zerock</groupId>
<artifactId>controller</artifactId>
<name>Spring_AtoZ</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>11</java-version>
<org.springframework-version>5.3.21</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>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.18.0</version>
</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.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</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>11</source>
<target>11</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>
생성한 패키지 및 클래스, root-context.xml파일 위치
Chef.java
package org.zerock.sample;
import org.springframework.stereotype.Component;
import lombok.Data;
@Component
//spring이 관리할 수 있게 알려주는 것.
//스프링에서 자신이 관리해야하는 대상 빈임을 표시하는 어노테이션
@Data
//Lombok의 setter, 생성자, toString() 메소드 자동 생성
public class Chef {
public Chef() {
// TODO Auto-generated constructor stub
}
}
Restaurant.java
package org.zerock.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.Setter;
@Component
@Data
//컴파일시, setter, 생성자 등을 만들어준다
public class Restaurant {
//객체간의 의존 관계 설정, 생성자 또는 setter를 이용
@Setter(onMethod_ = {@Autowired})
//@Autowired가 연결해준다.
//의존 주입(DI)
private Chef chef;
}
SampleTests.java
package org.zerock.sample;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
//JUnit 프레임워크의 실행방법을 확장시 사용하는 어노테이션.
//JUnit이 테스트를 진행하는 중에 사용할 애플리케이션 컨텍스트를
//생성하고 관리하는 작업을 진행한다
@RunWith(SpringJUnit4ClassRunner.class)
//
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
//지정된 경로의 root-context.xml로부터 필요한 클래스를 스프링의 빈으로 등록한다
@Log4j2//출력
public class SampleTests {
//@Autowired는 해당 인스턴스 변수가 스프링으로부터 자동으로
//주입해달라는 표시의 어노테이션이다.
@Setter(onMethod_= {@Autowired})
private Restaurant restaurant;
//JUit에서 아래의 메소드가 단위 테스트 대상임을 나타내는 어노테이션.
@Test
public void testExist() {
//restaurant 인스턴스 변수가 정상적으로 초기화 되었는지를 확인
//null 아니면, 정상적으로 DI가 이루어진 것이다
assertNotNull(restaurant);
log.info(restaurant);
log.info("--------------------");
log.info(restaurant.getChef());
}
}
SampleHotel.java
package org.zerock.sample;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@Component
@ToString
@Getter
//@AllArgsConstructor
@RequiredArgsConstructor
public class SampleHotel {
@NonNull
private Chef chef;
private Chef chef2;
// public SampleHotel(Chef chef) {//@AutoWired가 없음.//묵시적 생성자 주입이 가능=>@AutoWired를 생략가능
// this.chef=chef;
// }
}
HotelTest.java
package org.zerock.sample;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j2
public class HotelTest {
@Setter(onMethod_ = {@Autowired} )
private SampleHotel hotel;
@Test
public void testExists() {
//hotel 인스턴스 변수가 null이 아니라면, 묵시적 의존주입이 된 것이다.
assertNotNull(hotel);
log.info(hotel);
log.info("--------------------");
log.info(hotel.getChef());
}
}
root-context.xml을 열고 Namespaces에서 context를 체크한다
Source에서 코드를 추가한다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 최상위 루트 1개를 만들어두면 @Component가 있으면 알아서 bean을 만들어준다 -->
<context:component-scan base-package="org.zerock.sample">
</context:component-scan>
</beans>
저장을 하고 난 후 Beans Graph를 확인한다.
이클립스에서 반영하는 속도가 느리기 때문에 껐다가 다시 켜면 나올 수도 있다.
Run As에서 JUnit Test를 해본다.
정상적으로 실행된 경우 녹색 게이지 바가 보인다.
'2022-코딩 수업 정리' 카테고리의 다른 글
Spring 프로젝트 설정 코드 정리 (0) | 2022.10.13 |
---|---|
오라클과 이클립스 연결 (0) | 2022.10.11 |
스프링 환경 설정 (0) | 2022.10.05 |
게시판 만들기-3 (0) | 2022.10.05 |
게시판 만들기-2 (0) | 2022.10.01 |