STS 와 달리 Intellij에는 Spring legacy 프로젝트를 직접적으로 생성할 수 있는 템플릿이 존재하지 않는다. 이러한 이유로 Spring framework를 사용하기 위해 Maven의 Web application 구조에 맞추어 프로젝트를 구성해야 하지만, Intellij Ulitimate 라이선스에서 제공하는 Maven Archetype를 활용해 Spring Legacy 프로젝트를 좀 더 쉽게 구성할 수 있다.
1. Maven 프로젝트 생성

위 이미지 처럼 Maven Archetype을 Generators로 선택한 후 원하는 project 정보를 입력한다.
Catalog를 Maven Central로 변경한 뒤 maven 프로젝트를 위한 Archetype을 받아올 수 있도록 설정한다.
Archetype : org.apache.maven.archetypes:maven-archetype-webapp
2. Spring project로 설정파일 변경 및 생성

생성을 완료하면 위처럼 단순 maven 구조로 생성되며 이후 spring 프레임워크를 위한 설정 파일들을 작성하여 프로젝트를 구성한다.
※ 스프링 설정파일 (maven 구조내 java annotation 방식기반)
- 디렉토리 구조 설정
- pom.xml (프로젝트의 라이브러리 의존성 및 빌드 설정)
- web.xml (서블릿구조 설정)
- ApplicationConfig.java, ServletConfig.java (어플리케이션과 메인 서블릿에서 사용하는 설정)
2-1. 디렉토리 구조 설정

I. src/main 하위에 java 디렉토리 및 패키지를 생성한다.
II. webapp/WEB-INF 하위에 views 폴더를 생성하고 jsp 파일을 views 폴더 내부에서 관리한다.
2-2. pom.xml
* 아래는 예시일 뿐이며 라이브러리 버전의 경우 구성하고자하는 프로젝트의 성격에 맞추어 메이븐 레포지토리에서 적절한 버전을 찾아 설정한다.
[참고] 메이븐 레포지토리: https://mvnrepository.com/
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.jaewookmun</groupId>
<artifactId>annotation-config</artifactId>
<version>1.0-SNAPSHOT</version>
<name>annotation-config Maven Webapp</name>
<packaging>war</packaging>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>3.2.9.RELEASE</org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</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>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>7.0.70</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>annotation-config</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2-3. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://Java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.github.jaewookmun.annotationconfig.config.ApplicationConfig</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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.github.jaewookmun.annotationconfig.config.ServletConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- filter -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2-4. ApplicationConfig / ServletConfig
package com.github.jaewookmun.annotationconfig.config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
}
/**
* Spring 버전 5.0 (Spring Boot 2) 부터 추상클래스 WebMvcConfigurerAdapter의 사용은 Deprecated 된다. <br>
* [참고] - https://www.baeldung.com/web-mvc-configurer-adapter-deprecated <br>
* <br>
*
* WebMvcConfigurer 인터페이스를 직접 구현하여 사용하는 것이 권장된다.
*/
@Configuration
@EnableWebMvc
@ComponentScan(
basePackages = "com.github.jaewookmun.annotationconfig"
)
public class ServletConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
3. 스프링 구동 및 페이지 확인
@Controller
public class SampleController {
@RequestMapping(value = "/hello-page", method = RequestMethod.GET)
public String helloPage() {
return "index";
}
}
구동 로그

페이지 화면

[참고]
- github
https://github.com/JaewookMun/programming-exercise/tree/main/spring-legacy/annotation-config
- Java 기반 Container 설정 방식 : AnnotationConfigApplicationContext (Annotation 기반)
https://docs.spring.io/spring-framework/reference/core/beans/java/instantiating-container.html
Instantiating the Spring Container by Using AnnotationConfigApplicationContext :: Spring Framework
In much the same way that Spring XML files are used as input when instantiating a ClassPathXmlApplicationContext, you can use @Configuration classes as input when instantiating an AnnotationConfigApplicationContext. This allows for completely XML-free usag
docs.spring.io
'Spring' 카테고리의 다른 글
[multipart/form-data] - MultipartFile과 JSON 함께 받기 (HttpMediaTypeNotSupportedException가 발생하는 이유) (0) | 2023.11.04 |
---|---|
[Spring Security] session 동시 접속자 수 제한 (1) | 2023.04.16 |
[환경 설정] 데이터 베이스 (DB) (H2) (0) | 2023.01.29 |
스프링 웹프로젝트 간편 생성 - spring initializr 사용하기 (0) | 2023.01.25 |
[JPA] JPA를 통한 데이터 조회 (Entity, DTO) (0) | 2022.04.24 |