본문 바로가기
☕Java/Spring

[20210714] Spring + MyBatis를 이용한 게시판 4 - spring-app.xml, spring-controller.xml

by 캔 2021. 7. 15.

spring-app.xml: 스프링 웹 프로젝트의 mvc 구조에 대한 설명을 담고 있다. 애너테이션(annotation)을 사용하기 위해 <mvc:annotation-driven /> 태그를 사용하고, 기본 서블릿 핸들러를 핸들러 매핑으로 사용하며, 뷰 결정자(view resolvers)는 컨트롤러가 반환한 값 앞에 '/views/'를 붙이고 뒤에는 '.jsp'를 붙인 뷰를 사용할 것이라고 설정하였다.

<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<mvc:annotation-driven />
	<mvc:default-servlet-handler />
	<mvc:view-resolvers>
		<mvc:jsp prefix="/views/" suffix=".jsp" />
	</mvc:view-resolvers>
</beans>

spring-controller.xml: 스프링 웹 개발을 위한 빈 객체들을 담고 있다. DAO, SQLSessionFactorybean, Property, DataSource, SqlSessionTemplate을 스프링 컨테이너(또는 IoC 컨테이너)에 담는다.

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:component-scan base-package="newProject.*"></context:component-scan>

<bean id="dao" class="newProject.dao.BoardDao">
	<property name="ss" ref="sqlSessionTemplate" />
</bean>

<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- dataSource : DB 연결정보 -->
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml" />
	</bean>
	
	
	<bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="/WEB-INF/db.properties" />	
	
	</bean>
	
	<!-- DB연결정보 bean -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />	
	</bean>
	
	<!-- SqlSessionTemplate 객체 -->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<!-- SqlSessionTemplate는 생성자가 필요함 -->
		<constructor-arg ref="sqlSessionFactoryBean"></constructor-arg>
	</bean>
</beans>