๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

Spring root-context.xml ๋œฏ์–ด๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ŸŒฑSPRING

Spring root-context.xml ๋œฏ์–ด๋ณด๊ธฐ

jhj.sharon 2023. 5. 3. 18:02
๋ฐ˜์‘ํ˜•

 

 

 

1. root-context.xml 

  • web.xml ํŒŒ์ผ์—์„œ ๊ฐ€์žฅ ๋จผ์ € ์ฝ์–ด๋“ค์ด๋Š” ์„ค์ • ํŒŒ์ผ
  • ํ”„๋กœ์ ํŠธ ์ „๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ž์›์„ ์ƒ์„ฑํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ํŒŒ์ผ
  • DB ์—ฐ๊ฒฐ ๊ด€๋ จ ์ •๋ณด, ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ, ํŒŒ์ผ ์—…๋กœ๋“œ ๋“ฑ์„ ์ž‘์„ฑํ•œ๋‹ค.
  • Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋Š” ์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ๋กœ, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•„์š”ํ•œ ๊ณณ์—์„œ DI(Dependency Injection)๋กœ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋Š” ์Šคํ”„๋ง์˜ ํ•ต์‹ฌ์ ์ธ ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ์€ root-context.xml, servlet-context, applicationContext.xml ๋“ฑ์ด ์žˆ๋‹ค. 
  • ์ด ์ค‘ root-context.xmlํŒŒ์ผ์€ ์ฃผ๋กœ ์ „์—ญ์ ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ • ์ •๋ณด๋ฅผ ์ •์˜ํ•˜๊ณ   servlet-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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
		
		 
	<!-- 1.DBCP ์‚ฌ์šฉ์„ ์œ„ํ•œ DataSource๋ฅผ bean ๋“ฑ๋ก -->
	
	<!-- DataSource: java์—์„œ Connection pool์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค(์ปค๋„ฅ์…˜์˜ ์ƒ์œ„๋ฒ„์ „) -->
	<!-- destroy-method="close" : ์ฃผ์–ด์ง„ ์„ธ์…˜์„ ์ž๋™์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„ค์ • -->
	<bean id="realDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
	
	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
		<property name="username" value="community"/>
		<property name="password" value="1234"/>
		
		<!-- SQL์ด ์ˆ˜ํ–‰๋œ ํ›„ ์ž๋™์œผ๋กœ Commit ๋˜๋Š”๊ฒƒ์„ ์ง€์ • -->
		<property name="defaultAutoCommit" value="false"/>
		
		<!-- ์ปค๋„ฅ์…˜ ํ’€ ์„ค์ • -->
		<property name="initialSize" value="10" /> <!-- ์ดˆ๊ธฐ ์ปค๋„ฅ์…˜ ์ˆ˜, ๊ธฐ๋ณธ 0 -->
		<property name="maxTotal" value="50" /> <!-- ์ตœ๋Œ€ ์ปค๋„ฅ์…˜ ์ˆ˜, ๊ธฐ๋ณธ 8 -->
		<property name="maxIdle" value="20" /> <!-- ์œ ํœด ์ƒํƒœ๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ์ปค๋„ฅ์…˜ ์ตœ๋Œ€ ์ˆ˜, ๊ธฐ๋ณธ 8 -->
		<property name="minIdle" value="10" /> <!-- ์œ ํœด ์ƒํƒœ๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ์ปค๋„ฅ์…˜ ์ตœ์†Œ ์ˆ˜, ๊ธฐ๋ณธ 0 -->
		<property name="maxWaitMillis" value="-1" /> <!-- ์˜ˆ์™ธ ๋ฐœ์ƒ ์ „ ์ปค๋„ฅ์…˜์ด ๋ฐ˜ํ™˜ ๋  ๋–„ ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„(ms), ๊ธฐ๋ณธ -1(๋ฌด๊ธฐํ•œ) -->

	
	</bean>
	
	<!-- 
		log4j๋ฅผ ์ด์šฉํ•œ SQL ๋กœ๊ทธ ์ถœ๋ ฅ์šฉ bean ์ƒ์„ฑ
	
		* realDataSource๋ฅผ ์ด์šฉํ•ด์„œ SQL ์ˆ˜ํ–‰ ๋‚ด์šฉ, ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์—ญํ• 
		-> realDataSource๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ์—ญํ• 
	 -->
	 <bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
	 	<constructor-arg ref="realDataSource" />
	 	
	 	<property name="logFormatter">
	 		<bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
	 			<property name="loggingType" value="MULTI_LINE"/>
	 			<property name="sqlPrefix" value="[SQL]" />
	 		</bean>
	 	</property>
	 </bean>

	
		<!-- Mybatis ๊ด€๋ จ Bean ์ƒ์„ฑ -->
		
		<!-- SqlSession : sql๊ตฌ๋ฌธ์„ DB์— ์ „๋‹ฌ, ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด 
				SqlSessionFactory : SqlSession์„ ๋งŒ๋“œ๋Š” ๊ฐ์ฒด 
				sqlSessionFactoryBean : mybatis ์„ค์ • ํŒŒ์ผ(mybatis-config.xml)๊ณผ Connection Pool ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ SqlSessionFactory๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ์ฒด 
				sqlSessionTemplate : SqlSession ๊ฐ์ฒด์— ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ์—ญํ• ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋Š” ๊ฐ์ฒด -->
	
		<!-- ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค SqlSession ๋“ฑ๋กํ•˜๊ธฐ (xml ๋ฐฉ์‹์œผ๋กœ bean ๋“ฑ๋ก) -->
		<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
			<!-- mybatis-config.xml ์„ค์ • ๋ถˆ๋Ÿฌ์˜ค๊ธฐ -->
			<property name="configLocation" value="classpath:mybatis-config.xml" />
			<property name="dataSource" ref="dataSource" />
		</bean>
	
		<!-- SqlSessionTemplate : ๊ธฐ๋ณธ SQL ์‹คํ–‰ + ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ์—ญํ• ์„ ํ•˜๋Š” SqlSession์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฐ์ฒด(Spring bean์œผ๋กœ ๋“ฑ๋กํ•ด์•ผํ•จ.) -->
		<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
			<constructor-arg ref="sqlSessionFactoryBean" />
		</bean>
	
		<!-- ์Šคํ”„๋ง์—์„œ ์‚ฌ์šฉํ•˜๋Š” proxy๋ฅผ ์ด์šฉํ•œ ํŠธ๋žœ์žญ์…˜ ์ œ์–ด๊ฐ€ ์•ˆ๋  ๊ฒฝ์šฐ ์ถ”๊ฐ€์ ์ธ ํŠธ๋žœ์žญ์…˜ ๋งค๋‹ˆ์ €๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๋ฌธ์ œ ํ•ด๊ฒฐ -->
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource" />
		</bean>
	
		
	<!-- 	<tx:annotation-driven proxy-target-class="true"/> -->
		<tx:annotation-driven transaction-manager="transactionManager"/>
		
		
		<!-- 
	ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ MutipartResolver ๊ตฌํ˜„์ฒด CommonsMultipartResolver  bean ๋“ฑ๋ก 
	-> CommonsMultipartResolver๋ฅผ bean์œผ๋กœ ๋“ฑ๋กํ•˜๋ฉด
		multipart/form-data ํ˜•์‹์œผ๋กœ ์š”์ฒญ ์‹œ  input type="file" ํƒœ๊ทธ๋ฅผ ์ž๋™์ ์œผ๋กœ ์ธ์‹ํ•˜์—ฌ MultipartFile ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ 
		ํŒŒ์ผ ์™ธ์˜ ๋ฐ์ดํ„ฐ(์ •์ˆ˜, ๋ฌธ์ž์—ด ๋“ฑ์˜ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ)๋Š” ๊ธฐ์กด์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ(MultipartRequest ํ•„์š” ์—†์Œ)
	-->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		 <property name="maxUploadSize" value="104857600"/>
		 <property name="maxUploadSizePerFile" value="104857600"/>
		 <property name="maxInMemorySize" value="104857600"/>
	</bean>
	<!-- 
		104857600 byte == 100MB
		20971520 byte == 20MB
		
		maxUploadSize 
			: ํ•œ ์š”์ฒญ๋‹น ์—…๋กœ๋“œ๊ฐ€ ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ์šฉ๋Ÿ‰์„ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์„ค์ •.
			-1 ์€ ์ œํ•œ์ด ์—†๋‹ค๋Š” ๋œป์œผ๋กœ ์ด ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์„๋•Œ ๊ธฐ๋ณธ๊ฐ’.
		
		maxUploadSizePerFile
		 : ํ•œ ํŒŒ์ผ๋‹น ์—…๋กœ๋“œ๊ฐ€ ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ์šฉ๋Ÿ‰์„ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์„ค์ •.
			-1 ์€ ์ œํ•œ์ด ์—†๋‹ค๋Š” ๋œป์œผ๋กœ ์ด ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์„๋•Œ ๊ธฐ๋ณธ๊ฐ’.
			
		maxInMemorySize 
			: ๋””์Šคํฌ์— ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€ํ•˜๋„๋ก 
			ํ—ˆ์šฉํ•˜๋Š” ๋ฐ”์ดํŠธ ๋‹จ์œ„์˜ ์ตœ๋Œ€ ์šฉ๋Ÿ‰์„ ์„ค์ •.
			
	 		์‚ฌ์ด์ฆˆ๊ฐ€ ์ด๋ณด๋‹ค ํด ๊ฒฝ์šฐ ์ด ์‚ฌ์ด์ฆˆ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๋Š” ํŒŒ์ผ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. 
			 ๊ธฐ๋ณธ๊ฐ’์€ 10240 ๋ฐ”์ดํŠธ.
	 -->	
</beans>

 

 

 

2. root-context.xml  ๋œฏ์–ด๋ณด๊ธฐ

  • Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•  ๋นˆ(bean) ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค.
  • ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” DataSource / log4j๋ฅผ ์ด์šฉํ•œ SQL ๋กœ๊ทธ ์ถœ๋ ฅ์šฉ ๊ฐ์ฒด  / SqlSessionTemplate ๋“ฑ์„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  ์žˆ๋‹ค.

 

 

1. DBCP ์‚ฌ์šฉ์„ ์œ„ํ•œ DataSource

 

  • DataSource: java์—์„œ Connection pool์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค(์ปค๋„ฅ์…˜์˜ ์ƒ์œ„๋ฒ„์ „). ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ์ปค๋„ฅ์…˜ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ , ์ปค๋„ฅ์…˜์„ ์ƒ์„ฑ, ํ’€๋ง, ํ•ด์ œ ๋“ฑ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • destroy-method="close" : ์ฃผ์–ด์ง„ ์„ธ์…˜์„ ์ž๋™์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„ค์ •
  • DBCP๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ๋ถ€ํ•˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ์ปค๋„ฅ์…˜์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด 'id=realDataSource'๋ฅผ ์„ค์ •ํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ๋“ฑ๋กํ–ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋“ฑ๋ก๋œ DataSource๋Š” ๋‹ค๋ฅธ Bean์—์„œ DI(Dependency Injection)์„ ํ†ตํ•ด ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

2.  log4j๋ฅผ ์ด์šฉํ•œ SQL ๋กœ๊ทธ ์ถœ๋ ฅ์šฉ bean ์ƒ์„ฑ

 

  • SQL๋ฌธ์˜ ๋กœ๊ทธ ์ถœ๋ ฅ์„ ์œ„ํ•œ ์„ค์ •์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค
  • Log4jdbcProxyDataSource๋Š” ์‹ค์ œ๋กœ DB์™€ ์—ฐ๊ฒฐ๋œ dataSource(realDataSource)๋ฅผ ๋Œ€์‹ ํ•˜์—ฌ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด SQL๋ฌธ์ด ์ˆ˜ํ–‰๋  ๋•Œ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • Log4jdbcCustomFormatter๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•  ๋กœ๊ทธ์˜ ํ˜•์‹์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, loggingType์€ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • MULTI_LINE์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ค„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ถœ๋ ฅ๋œ๋‹ค.
  • sqlPrefix๋Š” ๋กœ๊ทธ ์•ž์— ์ถœ๋ ฅ๋  ๋ฌธ์ž์—ด์„ ์ •์˜ํ•œ๋‹ค. 
  • ์•„๋ž˜๋Š” ์ถœ๋ ฅ ์˜ˆ์‹œ

 

 

 

 

3. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค(MyBatis) ๊ด€๋ จ Bean์„ ์ƒ์„ฑ

 

  • ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋Š” SQL Mapper ํ”„๋ ˆ์ž„์›Œํฌ๋กœ, JDBC๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์„ ๋Œ€๋ถ€๋ถ„ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.
  • SqlSessionFactory: SqlSession์„ ๋งŒ๋“œ๋Š” ๊ฐ์ฒด๋กœ 'mybatis-confing.xml' ์„ค์ • ํŒŒ์ผ๊ณผ Connection pool ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ SqlSessionFactory๋ฅผ ๋งŒ๋“ค์–ด ๋‚ธ๋‹ค.
  • SQLSessionTemplate : SqlSession์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฐ์ฒด๋กœ ๊ธฐ๋ณธ SQL ์‹คํ–‰๊ณผ ํŠธ๋žœ์žญ์…˜ ๊ณผ๋ฆฌ ์—ญํ• ์„ ํ•œ๋‹ค.
  • transactionManage : ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ์ฒด์ด๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ tx:annotation-driven ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด transactionManager๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ์žˆ๋‹ค. ์ด ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜๋ฉด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„ํŽธํ•˜๊ฒŒ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.
๋ฐ˜์‘ํ˜•
Comments