스프링 프레임워크 기본 환경설정
스프링 | 2020.10.18 22:20
나의 개발환경은 윈도우즈10에 STS를 사용한다.
JDK 1.8.x 이며 db는 mysql 이다.
먼저 pom.xml을 살펴보자.
DB처리를 하는 부분이다.
예전에 jdbc 를 이용해 db 접속하고 try, catch, exception 처리하면서 코드가 길고 더러워지는 경험이 많았었는데 mybatis 같은 persistence 프레임워크를 손쉽게 사용할 수 있다는 것 자체가 스프링을 사용하는 목적의 반은 이뤘다고 본다.
개발서버는 mysql을, 실서버는 mariadb를 사용할 것이다.
<!-- DB처리 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.0</version>
</dependency>
json 처리를 위한 라이브러리다.
ajax를 사용하기 위해서는 필수적으로 설치해야 한다고 보면 된다.
<!--JSON-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
@ModelAttribute와 BindingResult를 이용한 폼객체 검증에 사용된다.
<!-- validation -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.2.Final</version>
</dependency>
파일업로드와 이미지 업로드시 썸네일 생성을 위한 라이브러리들이다.
<!-- 파일업로드 -->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 이미지 업로드(썸네일 처리) -->
<!-- https://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib-->
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
<!-- image EXIF read -->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.9.1</version>
</dependency>
스프링 시큐리티를 이용한 인증처리에 관한 라이브러리
<!-- 스프링 시큐리티적용 구간 -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.3.4.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>5.3.4.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>5.3.4.RELEASE</version>
</dependency>
개발시 쿼리확인을 위한 것이다.
<!-- 개발시 log4j 에서 쿼리보기 위해(실서버 적용시 주석처리) -->
<dependency>
<groupId>org.lazyluke</groupId>
<artifactId>log4jdbc-remix</artifactId>
<version>0.2.7</version>
</dependency>
기본적인 라이브러리에 위에 나열한 것들이 innu.pe.kr에 사용된 대표적인 라이브러리다.
간혹, 필요한 기능을 구현해야 할 필요가 있을때는 왠만하면 개발 완료된 것들을 가져다 사용하는것을 추천한다.
예전에는 내가 직접 최적화해서 만들어 보겠노라고 큰소리 뻥뻥쳤지만 지나고 보면 참 어리석은 행동이다.
세상에는 나보다 월등히 뛰어난 개발자들이 많으며 그들을 믿고 그대로 사용하는 것은 프로그램의 질적향상과 생산성에 직결된다. 더구나 공짜로 제공해주니 마냥 고마울 따름이다.
스프링에서 필요한 왠만한 라이브러리는 mvnrepository.com에서 찾아볼 수 있다.
web.xml은 사실 별거 없다.
왠만한 내용은 똑같고 한글 인코딩 처리같은 하위레벨의 원시적인 처리만을 담당한다.
<!-- 한글 인코딩 처리 -->
<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>
또한, 스크링 시큐리티 설정파일을 추가한 부분은 다음과 같다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/appServlet/security-config.xml
</param-value>
</context-param>
servlet-context.xml 는 현재 사용하고 있는 것을 그대로 올려 본다.
<?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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- <resources mapping="/resources/**" location="/resources/" /> -->
<resources mapping="/ckeditor/**" location="/resources/ckeditor/" />
<resources mapping="/images/**" location="/resources/images/" />
<resources mapping="/assets/**" location="/resources/assets/" />
<!-- 업로드 디렉토리 -->
<resources mapping="/upload/**" location="/resources/upload/" />
<!-- <resources mapping="/upload/**" location="/home/gogisnim/WEB_SITE/blog/upload/" />-->
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<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>
<context:component-scan base-package="kr.pe.innu" />
<!-- job Scheduler : 트래킹 로그 집계 -->
<task:scheduler id="jobScheduler" pool-size="10" />
<task:annotation-driven scheduler="jobScheduler" />
<!-- DB관련 (실서버용) -->
<!--
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<beans:property name="url" value="jdbc:mariadb://127.0.0.1:3306/blog" />
<beans:property name="username" value="root" />
<beans:property name="password" value="1212" />
</beans:bean>
-->
<!-- DB관련 (개발용) -->
<!-- log4j.xml, pom.xml 수정해야 함(수정안하면 성능 떨어짐) -->
<beans:bean id="dataSourceSpied" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/blog?useSSL=false&serverTimezone=UTC" />
<beans:property name="username" value="root" />
<beans:property name="password" value="1212" />
</beans:bean>
<beans:bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
<beans:constructor-arg ref="dataSourceSpied" />
<beans:property name="logFormatter">
<beans:bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
<beans:property name="loggingType" value="MULTI_LINE" />
<beans:property name="sqlPrefix" value="SQL : "/>
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="mapperLocations" value="classpath:mapper/*.xml" />
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<beans:bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<beans:constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</beans:bean>
<!-- 업무관련 -->
<beans:bean name="postService" class="kr.pe.innu.service.PostServiceImpl" />
<beans:bean name="postDao" class="kr.pe.innu.dao.PostDaoImpl" />
<beans:bean name="fileService" class="kr.pe.innu.service.FileServiceImpl" />
<beans:bean name="fileDao" class="kr.pe.innu.dao.FileDaoImpl" />
<beans:bean name="categService" class="kr.pe.innu.service.CategServiceImpl" />
<beans:bean name="categDao" class="kr.pe.innu.dao.CategDaoImpl" />
<beans:bean name="commentService" class="kr.pe.innu.service.CommentServiceImpl" />
<beans:bean name="commentDao" class="kr.pe.innu.dao.CommentDaoImpl" />
<beans:bean name="userService" class="kr.pe.innu.manager.service.UserServiceImpl" />
<beans:bean name="userDao" class="kr.pe.innu.manager.dao.UserDaoImpl" />
<beans:bean name="trackService" class="kr.pe.innu.manager.service.TrackServiceImpl" />
<beans:bean name="trackDao" class="kr.pe.innu.manager.dao.TrackDaoImpl" />
<!-- 업무관련 -->
<!-- 파일업로드 -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="10000000" />
</beans:bean>
<!-- 인터셉터 -->
<!-- 로그인 체크(preHandle) -->
<beans:bean id="LoginCheckInterceptor" class="kr.pe.innu.interceptor.LoginCheckInterceptor" />
<interceptors>
<interceptor>
<mapping path="/manager/*" />
<mapping path="/manager/**" />
<exclude-mapping path="/manager/join"/>
<exclude-mapping path="/manager/login"/>
<exclude-mapping path="/manager/login_ok"/>
<exclude-mapping path="/manager/logout"/>
<beans:ref bean="LoginCheckInterceptor" />
</interceptor>
</interceptors>
<!-- 카운터(afterCompletion) -->
<beans:bean id="CounterInterceptor" class="kr.pe.innu.interceptor.CounterInterceptor" />
<interceptors>
<interceptor>
<mapping path="/list" />
<mapping path="/view" />
<mapping path="/view/**" />
<mapping path="/test" />
<beans:ref bean="CounterInterceptor" />
</interceptor>
</interceptors>
</beans:beans>
"스프링" 카테고리의 다른 글
Spring과 Firebase를 이용한 주식 알리미 구현 (0) | 2021.02.11 |
SpringMVC를 이용한 게시판 구현 간략 정리 (0) | 2021.01.30 |
스프링 프레임워크 기본 환경설정 (0) | 2020.10.18 |
스프링 프레임워크 (0) | 2020.10.13 |