본문 바로가기
개발 관련 일지/학습 일지

Spring 웹 개발 기본 구조 연결 설정법(Eclipse)

by 최준규 2023. 5. 8.

스프링(Spring) 기본 설정 순서 

 

  1. web.xml
  2. *-context.xml  //application-context.xml, servlet-context.xml, root-context.xml 
  3. globals.properties  //Java Resources/src/main/resources 내부에 globals.properties.를 생성

 

web.xml 기본 설정

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

	<!-- 기본 제공 context 연결 설정 -->
	<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>

	<!-- context.xml 연결  -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
            // appServlet 내부에 위치하는 -context.xml이 붙은 모든 파일 정보를 가져와서 연결 
			<param-value>/WEB-INF/spring/appServlet/*-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
        // /*가 아닌 /인 이유는 tomcat Servers폴더 내부의 web.xml 기본 설정에 이미 servlet-mapping이
        // default url 매핑과 jsp 매핑이 이미 되어 있는데 /*로 모든걸 가져와 처리하려고 하니 오류가 발생한다.
		<url-pattern>/</url-pattern> 
	</servlet-mapping>
	
    <!-- UTF-8로 필터링 안했을 경우 웹에서 한글 깨짐문제 발생 -->
	<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> // UTF-8로 필터링 설정
		</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> // /뒤에 붙은 모든 웹 url에 필터링 기능 매핑
	</filter-mapping>
</web-app>

 

*-context.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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
	
	<!-- DB연결에 값을 보내기 위한 properties 외부 설정 
    locations 프로퍼티를 다른곳에서 ${변수명} 으로 사용 할수 있다 ex)${jdbc.url}-->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
            	<!-- classpath:는 [프로젝트명]/src/main/resources, java 랑 동일 
                [프로젝트명]/src/main/resources, java/Properties/globals.properties로 연결 -->
				<value>classpath:Properties/globals.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- DB연결 
    위에서 설정한 properties를 ${변수명}으로 설정하여 변수명에 맞는 값 가져오기-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- Mapper 연결 mybatis  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:Mapper/*/*Mapper.xml">
		</property>
	</bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> 
    </bean>
    
    <!-- interceptors 연결 class에 적힌 자바파일로 가서 입력해둔 코드가 실행됨 -->
    <mvc:interceptors>
    	<mvc:interceptor>
    		<mvc:mapping path="/**"/>
    			<bean id="commonInterceptor" class="com.hsmart.login.interceptor.loginInterceptor"/>
    	</mvc:interceptor>
    </mvc:interceptors>
</beans>

 

properties 설정 (microsoft sqlServer 설정법)

jdbc.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver <!-- microsoft sql 설정법 -->
jdbc.url = jdbc:sqlserver://[DB IP주소];DatabaseName=[DB 이름];encrypt=true;trustServerCertificate=true;
jdbc.username = [DB 계정아이디]
jdbc.password = [DB 계정비밀번호]

 

mySQL 설정법 

jdbc.driverClassName = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://[DB IP주소];DatabaseName=[DB 이름];encrypt=true;trustServerCertificate=true;
jdbc.username = [DB 계정아이디]
jdbc.password = [DB 계정비밀번호]

댓글