프로젝트를 구성하는 방법은 여러 가지가 있겠지만, 여기선 우선 Maven으로 프로젝트를 생성하고 Spring MVC 프레임워크를 추가하여 기본 틀을 만든 뒤에 라이브러리를 Maven으로 관리할 수 있도록 설정할 것이다.
(이클립스와 최대한 개발 환경 맞추기...😥)
New Project에서 Maven을 선택하고 JDK 버전을 설정한 뒤, Archetype을
org.apache.maven.archetypes:maven-archetype-webapp
으로 선택한 뒤 생성한다.
다른 블로그들을 참고해보면 프로젝트에서
Add FrameWork Support ...
를 통해서Spring MVC
를 추가하는데, 나는 눈을 씻고 찾아봐도 안나와서dependency
에서 직접 추가했다.. 🤮https://mvnrepository.com/ 에서 필요한
dependency
를 찾아서 추가한다.
💡 종류가 여러 개 있어서 뭘 추가해야하는지 모를 땐 그냥 많이 다운받은 걸로 하면 된다 !
pom.xml
springMVC, tomcat, jstl, jsp, servelt 추가<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jsp-api --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <version>8.5.88</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.5.88</version> </dependency> </dependencies>
pom.xml
에dependency
를 작성하고 나면 우측에 Maven 마크가 뜨는데 누르면 알아서 라이브러리에 추가해준다.
Spring MVC에서 기본적으로 설정해야 하는 dependency
slf4j
https://mvnrepository.com/artifact/org.slf4j/slf4j-api <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency>
lombok
https://mvnrepository.com/artifact/org.projectlombok/lombok <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> <scope>provided</scope> </dependency>
logback-classic
https://mvnrepository.com/artifact/ch.qos.logback/logback-classic <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.10</version> </dependency>
jackson-databind
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.1</version> </dependency>
spring framework 관련 dependency
spring-framework-bom
https://mvnrepository.com/artifact/org.springframework/spring-framework-bom 이 dependency는 spring-framework의 version 관리를 해준다. 지금까지는 RELEASE의 최고 버전 사용 중. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>5.2.19.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency>
bom에서 따로 버전을 관리하기 때문에 이름만 변경해주면 동일하게 사용할 수 있다.
1. spring-web
2. spring-webmvc
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-`사용할 dependency명`</artifactId> </dependency>
spring-framework-bom을 사용하지 않을 경우
1. spring-web
https://mvnrepository.com/artifact/org.springframework/spring-web <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.19.RELEASE</version> </dependency>
2. spring-webmvc
https://mvnrepository.com/artifact/org.springframework/spring-webmvc <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.19.RELEASE</version> </dependency>
spring security 관련 dependency
spring-security-bom
https://mvnrepository.com/artifact/org.springframework.security/spring-security-bom 위의 spring-security-bom의 version 관리해준다. 원래 spring-framework-bom의 버전보다 낮은 버전을 사용하는 게 훨씬 안전하지만 둘 다 RELEASE 버전을 사용하고 있기 때문에 그냥 제일 높은 버전을 사용함. <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-bom</artifactId> <version>5.3.13.REALASE</version> <type>pom</type> </dependency>
bom에서 버전 관리를 하기 때문에 이름만 변경해주면 동일한 코드가 사용 가능하다.
spring-security-core
,spring-security-web
,
spring-security-config
,spring-security-taglibs
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-'사용할 dependency명'</artifactId> </dependency>
servlet-api
https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api 웹 관련 요청에 대하여 동적인 처리를 도와주는 기술 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
jsp-api
https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api java 언어를 기반으로 웹 서비스를 제공하기 위해 만든 기술 <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency>
jstl
https://mvnrepository.com/artifact/javax.servlet/jstl <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
SQL 관련 dependency
spring-jdbc
https://mvnrepository.com/artifact/org.springframework/spring-jdbc <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency>
commons-dbcp2
https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.9.0</version> </dependency>
spring-data-jpa
https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.3.9.RELEASE</version> </dependency>
hibernate-core
https://mvnrepository.com/artifact/org.hibernate/hibernate-core <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.6.5.Final</version> </dependency>
spring-orm
https://mvnrepository.com/artifact/org.springframework/spring-orm <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency>
ojdbc8
<dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>21.1.0.0</version> </dependency>
MyBatis 관련 dependency
mybatis
https://mvnrepository.com/artifact/org.mybatis/mybatis <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency>
mybatis-spring
https://mvnrepository.com/artifact/org.mybatis/mybatis-spring <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency>
pom.xml
전체 코드<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringPj</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringPj Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.0.0.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>4.0.0.RELEASE</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>4.0.0.RELEASE</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>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</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.13.1</version>
<scope>test</scope>
</dependency>
<!-- 추가 의존 라이브러리 -->
<!-- MyBatis 라이브러리 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Spring JDBC 라이브러리 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 파일업로드 처리를 위한 라이브러리 (SmartEditor 에서도 필요함)-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- json, xml 응답을 편하게 할수 있도록 도와 주는 라이브러리 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.7.1</version>
</dependency>
<!-- Aop 용 라이브러리 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.0</version>
</dependency>
<!-- Spring Security 관련 라이브러리 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 트렌젝션 처리를 위한 라이브러리 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 오라클 라이브러리 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.1.0.0</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>SpringPj</finalName>
</build>
</project>
pom
요소만 추가하면 기본적인 세팅이 끝난다. 이후에servlet-context.xml
,root-context.xml
,Controller.java
등은 개발 환경에 맞게 세팅하면 된다 !