250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Tomcat
- JavaScript
- PostgreSQL
- jpa
- 설정
- oracle
- Docker
- Core Java
- Source
- myBatis
- 문서
- ubuntu
- maven
- Python
- JDBC
- SpringBoot
- Thymeleaf
- 오픈소스
- Spring Boot
- Eclipse
- IntelliJ
- AJAX
- MSSQL
- error
- Exception
- STS
- spring
- Open Source
- git
- MySQL
Archives
- Today
- Total
헤르메스 LIFE
[Spring] 개발환경 구축 - DBCP Connection Test 본문
728x90
Spring 개발환경을 갑자기 구축하려고 하다보니, 막막합니다.
하나 하나 구축해 보겠습니다
개발환경
Spring 4.3.30.RELEASE
MAVEN 3.8.4
Logback 1.2.9
commons-fileupload 1.4
commons-io 2.6
commons-dbcp 1.4
postgresql driver 42.5.0
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SimpleSpring4</groupId>
<artifactId>SimpleSpring4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SimpleSpring4</name>
<description>Simple Spring4</description>
<properties>
<springframework.version>4.3.30.RELEASE</springframework.version>
<org.slf4j-version>1.7.30</org.slf4j-version><!-- 1.7.26 -->
<ch.qos.logback-version>1.2.9</ch.qos.logback-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${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>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<!-- Bridge -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback-version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId><!-- logback을 slf4j-api에서 사용함을 의미 -->
</exclusion>
</exclusions>
<scope>runtime</scope><!-- runtime 시 사용함 -->
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3</version>
</dependency>
<!-- For user input validation
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
-->
<!-- commons-xxxx -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</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</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<disply-name>SIMPLE-SPRING4</disply-name>
<!--
DispatcherServlet 클래스의 로드보다 먼저 동작하여
비즈니스 로직층을 정의한(classpath*:/spring/context-*.xml)
스프링 설정 파일을 로드한다.
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/context-*.xml</param-value>
</context-param>
<!-- Processes application requests -->
<!-- HTTP 프로토콜로 들어오는 모든 요청을 가장 먼저 받아 적합한 컨트롤러에 위임해주는 프론트 컨트롤러(Front Controller) -->
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/DispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring에서 지원하는 Request에 대한 파라메터 값을 인코딩하는 필터 -->
<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>
<!-- Session 시간 지정(Spring) / 분 -->
<!-- HttpOnly는 자바스크립트의 document.cookie를 이용 해 쿠키에 접속하는 것을 막는 옵션이다. -->
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<name>JSESSIONID</name>
<path>/</path>
<http-only>true</http-only>
</cookie-config>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
DispatcherServlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<mvc:annotation-driven />
<!-- 빈 설정을 어노테이션 기반으로 하겠다는 설정태그 -->
<context:component-scan base-package="simple">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- CSS, JS, 이미지 등의 자원은 거의 변하지 않기 때문에, 웹 브라우저에 캐시를 하면
네트워크 사용량, 서버 사용량, 웹 브라우저의 반응 속도 등을 개선할 수 있다. -->
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:resources location="/resources/**" mapping="/resources/"></mvc:resources>
<!--
Mulitipart Resolver
maxUploadSize는 최대 업로드 가능한 파일의 바이트 크기
maxInMemorySize는 디스크에 임시 파일을 생성하기 전 메모리에 보관할 수 있는 최대 바이트 크기
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="104857600" /> <!-- 10M -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1M -->
</bean>
<bean id="uploadPath" class="java.lang.String">
<constructor-arg value="C:/project/upload/"></constructor-arg>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- Encoding 설정 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- Reload Cache 설정 -->
<property name="cacheSeconds" value="5"/>
<!-- basenames 설정: 아래처럼 하면 WEB-INF 밑의 message 폴더 아래의 labels로 시작하는 모든 Property-->
<property name="basenames">
<list>
<value>/WEB-INF/messages/message</value>
<value>classpath:/messages/message</value>
</list>
</property>
</bean>
<!-- MessageSource를 사용하기 위한 Accessor 설정 -->
<bean id="messageSourceAccessor" class="org.springframework.context.support.MessageSourceAccessor">
<constructor-arg ref="messageSource"/>
</bean>
<!-- Default Location 설정 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="ko"></property>
</bean>
<!--
mvc:default-servlet-handler
요청 경로와 일치하는 컨트롤러를 찾는다.
컨트롤러가 존재하지 않으면, 디폴트 서블릿 핸들러에 전달한다.
DispatcherSerlvet이 처리하지 못한 요청을 DefaultSerlvet에게 넘겨주는 역할을 하는 핸들러
*.css와 같은 컨트롤러에 매핑되어 있지 않은 URL 요청은 최종적으로 Default Servlet에 전달되어 처리하는 역할
web.xml 파일의 dispatcher 가 / 의 경우 설정한다.
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-->
<mvc:default-servlet-handler />
<!-- beanNameResolver는 우선순위를 0으로 해서 ViewPage를 찾을 때 우선 탐색한다.
만약 BeanNameViewResolver에서 없으면 UrlBasedViewResolve에서 탐색한다. -->
<bean id="beanNameResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" /> <!-- 순서를 지정하는 property -->
</bean>
<bean id="fileDownload" class="simple.spring.view.FileDownloadView" />
</beans>
context-datasource.xml
<?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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/springboot" />
<property name="username" value="hermeswing" />
<property name="password" value="pass" />
<property name="initialSize" value="5" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
<property name="maxWait" value="30000" />
<property name="validationQuery" value="select 1" />
<!-- testWhileIdle:컨넥션이 놀고 있을때 -_-; validationQuery 를 이용해서 유효성 검사를 할지 여부. -->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
</bean>
</beans>
DBTestController.java
package simple.spring.basic.db.controller;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DBTestController {
private static Logger logger = LoggerFactory.getLogger(DBTestController.class);
@Autowired
private DataSource dataSource;
@RequestMapping(value = "/dbcpConnectionTest", method = RequestMethod.GET)
public ModelAndView dbcpConnectionTest( ModelMap model ) {
try ( Connection conn = dataSource.getConnection() ) {
logger.info("Connection :: {}", conn);
} catch (SQLException e) {
e.printStackTrace();
}
return new ModelAndView("index");
}
}
결과
https://hermeslog.tistory.com/497?category=302346
https://hermeslog.tistory.com/541
728x90
'Spring Framework' 카테고리의 다른 글
[Spring] 개발환경 구축 - 이미지 파일 뷰 (0) | 2023.01.20 |
---|---|
[Spring] 개발환경 구축 - 대용량 Excel 다운로드 (0) | 2023.01.19 |
[Spring] 개발환경 구축 - File Download (0) | 2022.12.28 |
[Spring] 개발환경 구축 - File Upload (1) | 2022.12.26 |
[Spring] 개발환경 구축 - MessagesSource (0) | 2022.12.22 |