본문 바로가기
Web Programming/Spring

[Spring] Bean 설정문서

by Classic! 2020. 8. 6.

[Bean 설정 문서]

본 글에서는 bean설정 문서에서 Presentation Layer, Service Layer, Persistence Layer 에 존재하는 Component(=bean)을 입력한다. 여기서 Spring framework가서버의 back-end부터 front-end까지 연결하는 과정을 살펴볼 수 있다.

 

- 서버에서 Spring flow 개괄

sqlMapConfig.xml > SqlSessionFactory > SqlSession > UserDAO(DAOImpl) > UserService(UserServiceImpl)

> Presentation Layer(Component)

 

 

[Bean 설정 문서 flow]

 

1) properties 파일 연결

<?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.0.xsd">
   
	<!-- wiring : Namespaces에서 context 설정 선행 -->
	<context:property-placeholder location="classpath:config/dbconn.properties"/>
</beans>

 

** context 설정

아래 이미지와 같이 xml문서 > Namespaces탭 > context 체크

 

 

2) Data Source Component(=bean) : Data Source 형식으로 DB 연결

<!-- 5개 Bean생성 -->
<!-- 1. Data Source API Bean -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="${jdbc.mysql.driver}"/>
	<property name="url" value="${jdbc.mysql.url}"/>
	<property name="username" value="${jdbc.mysql.username}"/>
	<property name="password" value="${jdbc.mysql.password}"/>			
</bean>

 

3) SqlSession Component(=bean) : SqlSessionFactory로 sqlMappingConfig.xml과 DataSource 넘겨주기

<!-- 2. SqlSessionFactory API Bean-->
<bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">	
	<property name="configLocation" value="classpath:config/SqlMapConfig.xml"/>
	<!-- config와 함께 dataSource도 가져오기 -->
    <property name="dataSource" ref="dataSource"/>
</bean>

 

4) DAO Component(=bean) : SqlSession과 DAO연결

<!-- 4. DAOImpl12 사용자정의 Bean -->
<bean id= "myBatisUserDAOImpl12" class="ibatis.services.user.impl.MyBatisUserDAOImpl12">
	<property name="sqlSession" ref="sqlSession"/>
</bean>

 

5) Service Component(=bean) : DAO와 Service 연결

<!-- 5. ServiceImpl12 사용자정의 Bean -->
<bean id= "myBatisUserServiceImpl12" class="ibatis.services.user.impl.MyBatisUserServiceImpl12">
	<property name="userDAO" ref="myBatisUserDAOImpl12"/>
</bean>

 

위와 같이 Bean 설정 문서를 작성하고 나면, SqlMapConfig.xml에서 DB 연결을 위한 파일 연결과 정보 입력 부분은 삭제할 수 있다.

SqlMapConfig.xml에는 VO Alias와 Mapping 정보만 남게 된다.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 1. properties 파일 연결 >>> 삭제 -->
//	<properties resource="config/dbconn.properties"/>

	<!-- 2. VO Alias -->
	<typeAliases>
		<typeAlias type="ibatis.services.domain.User" alias="user"/>
	</typeAliases>
	
	<!-- 3. DB 정보 >>> 삭제 -->
// 	<environments default="development">
//		<environment id="development">
//			<transactionManager type="JDBC"></transactionManager>
//			<dataSource type="UNPOOLED">
//				<property name="driver" value="${jdbc.mysql.driver}"/>
//				<property name="url" value="${jdbc.mysql.url}"/>
//				<property name="username" value="${jdbc.mysql.username}"/>
//				<property name="password" value="${jdbc.mysql.password}"/>			
//			</dataSource>
//		</environment>
//	</environments>
 
 
	<!-- 4. mapper -->
	<mappers>
		<mapper resource="sql/mybatis-userservice-mapping.xml"/>
	</mappers>
</configuration>

 


[코드 작성 시 사용한 labrary 항목]

버전에 따라, 혹은 라이브러리 미설치로 오류가 뜨는 경우가 있어 코드를 작성하면서 사용한 라이브러리를 이미지로 첨부한다. 모든 라이브러리를 필수적으로 설치해야하는 것은 아니다.

 

 

'Web Programming > Spring' 카테고리의 다른 글

[Spring] BeanNameViewResolver  (0) 2020.08.10
[Spring] Annotation  (0) 2020.08.06
[Spring] My Batis - SQL  (1) 2020.08.05
[Spring] MyBatis Data Access  (0) 2020.08.04
[Spring] Core Container  (1) 2020.08.03

댓글