Develop

[Spring] Dynamic Web Project (maven project로 설정하기) 본문

웹 개발/Spring

[Spring] Dynamic Web Project (maven project로 설정하기)

개발 기록 2024. 3. 4. 10:51

 

새프로젝트 만들기

Dynamic Web Project를 생성하여 maven으로 변경후 설정해주는 내용을 정리할 예정이다.


 

1. 새프로젝트 생성

 

 

File

=> New

=> Dynamic Web Project

 

 

 

 

Project name에 원하는 프로젝트 이름을 적고

Target runtime도 Tomcat 버전이 맞는지 체크해주자

나는 9를 써서 맞으니 따로 설정하진 않겠다.

 

 

next

 

 

 

체크박스 체크한 다음 Finish 누르면 끝

 

 

2. Maven Project로 변경

 

 

 

 

생성한 프로젝트에서 마우스 우클릭

=> Configure

 

 

 

=> 맨 밑에 있는 Convert to Maven Project

 

 

=> Finish

 

 

프로젝트 맨 밑에 target 폴더와 pom.xml 이 생겼다면 제대로 변환된 것이다.

 

 

*  Project Explore 보다 Package Explore 를 사용하여 보는 것을 추천한다

이유 : 물리적 구조가 아닌 논리적 구조로 표시해주기 때문에 

더 직관적으로 파일 구조를 볼 수 있다.

 

 

* 만약 Package Explore 가 없다면 

 

 

에 들어가서 찾으면 된다.

 

 

 

 

 

 

3. pom.xml에 라이브러리 추가하기

 

Maven 사이트에서 라이브러리 검색 후 추가하기

 

3-1. Spring Web MVC 라이브러리

 

Spring webmvc contains Spring’s model-view-controller (MVC) and REST Web Services implementation for web applications. It provides a clean separation between domain model code and web forms and integrates with all of the other features of the Spring Framework.

 

Spring webmvc는 웹 응용 프로그램을 위한 Spring의 모델 뷰 컨트롤러(MVC)와 REST Web Services 구현을 포함합니다. 도메인 모델 코드와 웹 양식 사이에 깨끗한 분리를 제공하고 Spring Framework의 다른 모든 기능과 통합됩니다.

 

Maven Repository: org.springframework » spring-webmvc (mvnrepository.com)

 

 

3-2. 그 외의 라이브러리를 포함한 전체 dependencies

 

<dependencies>
    <!-- Spring -->
    <!-- Spring Web -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!-- Spring Web MVC -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

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

    <!-- 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.17</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>


    <!-- @@@@@@@@@@외부 라이브러리 추가@@@@@@@@@@ -->
    <!-- jQuery -->
    <dependency>
        <groupId>org.webjars.bower</groupId>
        <artifactId>jquery</artifactId>
        <version>3.5.1 </version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.4.RELEASE</version>
        <scope>test</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.4.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.30</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 -->
    <dependency>
        <groupId>org.bgee.log4jdbc-log4j2</groupId>
        <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
        <version>1.16</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/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
    <dependency>
        <groupId>net.coobird</groupId>
        <artifactId>thumbnailator</artifactId>
        <version>0.4.8</version>
    </dependency>



    <!-- 파일 업로드 -->
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.4</version>
    </dependency>
    <!-- 파일 업로드 -->

    <!-- 암호화 -->
    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>

    <!-- @@@@@@@@@@외부 라이브러리 추가@@@@@@@@@@ -->
</dependencies>

 

 

4. 필요한 폴더 생성하기

 

spring3 에서 쓰는 legacy project 처럼 쓰려면 아래 사진과 같이

servlet-context.xml 과  root-context.xml 을 생성후 설정해주면 된다.

 

 

 

 

servlet-context.xml

(may 프로젝트 내용 복붙함)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:mvc="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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd">
 	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
 	      
	<!-- Enables the Spring MVC @Controller programming model -->
	<mvc:annotation-driven />
	
	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" /> 
	
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
		<beans:property name="prefix" value="/WEB-INF/views/"/>
		<beans:property name="suffix" value=".jsp"/>
	</beans:bean>
	
	<context:component-scan base-package="com.may.controller" />

</beans:beans>

 

 

root-context.xml

(may 프로젝트 내용 복붙함)

 

<?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 -->
	<!-- 시작 : hikariCP 설정 정보(DB 연결정보) -->
	<bean id="hikariConfig"
	 	  class="com.zaxxer.hikari.HikariConfig">	
		  <property name="driverClassName"
		  			value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"/>
		  <property name="jdbcUrl" 
		  			value="jdbc:log4jdbc:mysql://localhost:3306/test1?serverTimezone=Asia/Seoul&amp;useSSL=false&amp;allowPublicKeyRetrieval=true"/>
		  <property name="username" value="root"/>
		  <property name="password" value="1234"/>						
	</bean>
	<!-- 끝 : hikariCP 설정 정보(DB 연결정보) -->
	
	
	<!-- 시작 : hikariCP사용 디비 연결 DataSource -->
	<bean id="dataSource"
		  class="com.zaxxer.hikari.HikariDataSource"
		  destroy-method="close">
		  <constructor-arg ref="hikariConfig"/>
	</bean>
	<!-- 끝 : hikariCP사용 디비 연결 DataSource -->
	
	
	<!-- 시작 : SqlSessionFactory 객체 - 디비연결, 처리, sql 실행, mybatis 설정 -->
	<bean id="sqlSessionFactory"
		  class="org.mybatis.spring.SqlSessionFactoryBean">
		  <property name="dataSource" ref="dataSource"/>
		  <property name="configLocation" 
		  			value="classpath:/mybatis-config.xml"/>
		  <property name="mapperLocations" 
		  			value="classpath:mappers/**/*Mapper.xml"/>
	</bean>
	<!-- 끝 : SqlSessionFactory 객체 - 디비연결, 처리, sql 실행, mybatis 설정 -->
	
	<!-- 시작 : SqlSessionTemplate 객체 - 디비연결(자동), myBatis 설정, SQL실행, 자원해제, 트랜잭션 관리, 쓰레드 안정성 관리 -->
	<bean id="sqlSession"
		  class="org.mybatis.spring.SqlSessionTemplate" 
		  destroy-method="clearCache">
		  <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
	</bean>
	<!-- 끝 : SqlSessionTemplate 객체 - 디비연결(자동), myBatis 설정, SQL실행, 자원해제, 트랜잭션 관리, 쓰레드 안정성 관리 -->
	
	<!-- com.may.persistence 패키지 등록 -->
	<context:component-scan base-package="com.may.persistence"></context:component-scan>	
	<!-- com.ma.service 패키지 등록 -->
	<context:component-scan base-package="com.may.service"></context:component-scan>	


</beans>