[Spring] 2.Spring 기본 환경 세팅

백준호·2022년 10월 22일
0

스프링스터디

목록 보기
2/5
post-thumbnail

본격적인 Spring 개발에 앞서 몇가지 환경세팅이 필요하다.


### servlet-context.xml Context란, Bean들을 다루기 위해 우리가 설정할 수 있는 공간이다. servlet-context.xml은 스프링 MVC에서 스프링의 IoC 컨테이너 역할을 수행한다.
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/res/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/" />
		<beans:property name="suffix" value=".jsp" />
  1. resources (정적인 html문서 같은 웹 리소스들의정보를 기술하는 태그)의 값을 res 로 바꾼다. (명령어 축약을 위해)

  2. <beans:bean class...> (Controller가 Model를 리턴하고 DispatcherServlet이 jsp 파일을 찾을 때 쓰이는 정보를 기술하는 태그)
    에 /WEB-INF/view에서 view 삭제. (경로 설정시 편의를 위해)


DispatcherServlet

프로젝트를 처음 생성할 때 web.xml에 명시된 DispatcherServlet 설정 부분은 다음과 같다.

web.xml

<!-- encoding -->

    <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>
    </filter>
    <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

1.web.xml에 한글엔코딩 추가

메이븐 레파지토리

메이븐은 자바 프로젝트를 관리하는 툴로, 미리 작성된 xml 파일을 이용하여 라이브러리를 자동으로 다운로드하거나 프로젝트를 빌드해준다. pom.xml은 간단히 말하면 Maven의 빌드 정보를 담고 있는 파일로, POM(Project Object Model)을 설정하는 부분으로 프로젝트 내 빌드 옵션을 설정하는 부분이다.

pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
				
		<!-- mysql -->		
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.30</version>
</dependency>
				
		<!-- mybatis -->		
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>

		<!-- mybatis spring -->		
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
</dependency>

1. spring jdbc, mysql, mybatis, mybatis spring 레파지토리 코드를 추가한다. _(https://mvnrepository.com/ : 메이븐 레파지토리)_

root context

root-context는 공통 bean을 모은 공간이다. 프로젝트의 어플리케이션 영역 설정하고 스프링 MVC 설정과 관련된 여러 처리를 담당한다. JSP와는 관련없는 객체(Bean)을 설정 하고, View와 밀접하지 않은 정보를 기술하는 xml 파일이다.

<!-- Root Context: defines shared resources visible to all other web components -->
<!--1.datasource 설정: 드라이버 관련-->
<!--  Database Connection을 생성 -->
<!-- DB 계정 이름과 패스워드 입력 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/sist?serverTimezone=Asia/Seoul"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property> <!-- sql연결 -->
	</bean>

<!--2.sqlSessionFactory 설정-->

	<!-- classpath: 빌드시 컴파일된 class 파일들의 위치 경로 -->
        <!-- <property> 태그 -->
        <!--
        SqlSessionFactoryBean 클래스에서 선언된
        dataSource, configLocation, mapperLoations 
        변수들의 값을 할당 받는다!! 
        -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- config 경로 -->
		<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" /> <!-- mapper 경로 -->
	</bean>

<!-- <constructor-arg> -->
	<!-- sqlSession bean에 sqlSessionFactory 객체 주입-->
	<bean id="sqlSession"
		class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory" />
	</bean>

	<!-- transactionManager에 dataSource 객체 주입-->	
    <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>


profile
남들이 다 아는 걸 모를 수는 없지!

0개의 댓글