Ian's Archive 🏃🏻

thumbnail
스프링 Legacy Project구조
Spring
2025.01.13.

intellij로 새로운 프로젝트를 생성할 때 마다 Spring Legacy프로젝트 구조가 헷갈려 메모용으로 정리한다.

PostgreSQL과 MyBatis를 사용할 때 프로젝트 구조

전체 프로젝트 구조

copyButtonText
.
├── SpringMVC01.iml   //IntelliJ
├── mvnw              //Maven
├── mvnw.cmd          //Maven
├── pom.xml           --> Maven 프로젝트 설정 파일
├── src
│   ├── main          --> 작성되는 코드의 경로
│   │   ├── com         
│   │   │   └── example
│   │   │       └── chat
│   │   │           ├── controller
│   │   │           ├── model
│   │   │           ├── dao
│   │   │           └── service     
│   │   │               └── serviceImpl.xml
│   │   ├── resources     --> 설정 파일
│   │   |   └── com       
│   │   │       └── example
│   │   │           └── chat
│   │   │               └── demo     
│   │   │                   └── mapper             --> MyBatis Mapper모음
│   │   │                       └── HomeMapper.xml
│   │   └── webapp                                 --> 웹 애플리케이션 관련
│   │       ├── WEB-INF
│   │       │   ├── spring                         --> 스프링 설정
│   │       │   │   ├── appServlet
│   │       │   │   │   └── servlet-context.xml    --> 웹과 관련된 스프링 설정 파일
│   │       │   │   └── root-context.xml           --> 스프링 설정 파일
│   │       │   └── views
│   │       │       └── template.jsp    
│   │       ├── resources                          --> 웹앱 정적 리소스(js, css)
│   │       └── web.xml                            --> 웹앱 배포 설명자 파일 / Tomcat의 web.xml파일
│   └── test             
│       ├── java             --> 테스트 코드를 넣는 경로
│       └── resources        --> 테스트 관련 설정 파일 보관 경로
└── target             --> 빌드, 패키징 관련 (빌드 도구에 의해 자동 생성)           

pom.xml

copyButtonText
<?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>com.egis</groupId>
    <artifactId>prj</artifactId>
    <name>default-project</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>

    <properties>
        <java-version>11</java-version>
        <org.springframework-version>5.2.8.RELEASE</org.springframework-version>
        <org.mybatis-version>3.5.13</org.mybatis-version>
        <org.mybatis-spring-version>2.0.7</org.mybatis-spring-version>
        <org.slf4j-version>1.7.36</org.slf4j-version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <!--    <repositories>-->
    <!--        <repository>-->
    <!--            <id>oracle</id>-->
    <!--            <name>Oracle JDBC Repository</name>-->
    <!--            <url>http://repo.spring.io/plugins-release/</url>-->
    <!--        </repository>-->
    <!--    </repositories>-->

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <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>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework-version}</version>
            <scope>test</scope>
        </dependency>

        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${org.mybatis-version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${org.mybatis-spring-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>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.20.0</version>
        </dependency>
        <!-- MyBatis Log -->
        <dependency>
            <groupId>org.bgee.log4jdbc-log4j2</groupId>
            <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
            <version>1.16</version>
        </dependency>

        <!-- Database -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>5.0.1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

        <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

root-context.xml

db설정과 MyBatis설정이 들어가 있다.

copyButtonText
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="
        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- HikariCP Configuration -->
	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
		<property name="driverClassName" value="org.postgresql.Driver"/>
		<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres"/>
		<property name="username" value="postgres"/>
		<property name="password" value="egis"/>
		<property name="maximumPoolSize" value="10"/>
		<property name="minimumIdle" value="2"/>
		<property name="idleTimeout" value="30000"/>
		<property name="connectionTimeout" value="20000"/>
	</bean>

	<!-- HikariCP DataSource -->
	<bean id="datasource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
		<constructor-arg ref="hikariConfig"/>
	</bean>

	<!-- MyBatis SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="datasource"/>
		<property name="mapperLocations" value="classpath*:com/example/demo/mapper/*.xml"/>
		<property name="typeAliasesPackage" value="com.example.demo.model"/>
	</bean>

	<!-- SqlSessionTemplate 설정 -->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory" />
	</bean>

</beans>

servlet-context.xml

servlet설정과 jsp설정이 들어가 있다.

copyButtonText
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
			 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			 xmlns:beans="http://www.springframework.org/schema/beans"
			 xmlns:context="http://www.springframework.org/schema/context"
			 xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!-- Enable Spring MVC annotations (@Controller, @RequestMapping, etc.) -->
	<annotation-driven/>

	<!-- Static Resources Handling -->
	<resources mapping="/resources/**" location="/resources/"/>

	<!-- View Resolver -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/"/>
		<beans:property name="suffix" value=".jsp"/>
	</beans:bean>

	<!-- Component Scanning -->
	<context:component-scan base-package="com.example.chat"/>

</beans:beans>

web.xml

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

    <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> <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Reference

[Spring] Spring Legacy Project 생성 (IntelliJ)
[Spring] Legacy Project구조와 설정하기
[Spring] Spring Legacy Project 디렉토리 구조 (+ 추가 중)

Thank You for Visiting My Blog, I hope you have an amazing day 😆
© 2023 Ian, Powered By Gatsby.